Python Instance Types

December 21, 2024 note-to-self python

Python's gettype or instanceof is isinstance().

isinstance() can be used to check for a variety of object types in Python. Here are some common examples:

Basic Data Types:

  • int: Integer
  • float: Floating-point number
  • str: String
  • bool: Boolean (True or False)
  • complex: Complex number

Container Types:

  • list: List
  • tuple: Tuple
  • dict: Dictionary
  • set: Set

Other Common Types:

  • NoneType: NoneType (represents the absence of a value)
  • range: Range object
  • bytes: Bytes object
  • bytearray: Bytearray object
>>> var = 'foo'
>>> print(isinstance(var, (str, int)))
True
>>> print(isinstance(var, (tuple, int)))
False