typeExpert Examples

Declares a type alias (3.12+)

type internals and the type/object bootstrap

The circular relationship between type and object.

python
# The fundamental bootstrap:
# - type is an instance of itself
# - type is a subclass of object
# - object is an instance of type

print(f"type(type): {type(type)}")
print(f"type(object): {type(object)}")
print(f"isinstance(type, object): {isinstance(type, object)}")
print(f"isinstance(object, type): {isinstance(object, type)}")
print(f"issubclass(type, object): {issubclass(type, object)}")

# type.__bases__
print(f"\ntype.__bases__: {type.__bases__}")
print(f"object.__bases__: {object.__bases__}")

# MRO of type
print(f"type.__mro__: {type.__mro__}")

# This circular relationship is bootstrapped in C code
# and cannot be recreated in pure Python
print(f"\ntype is type(type): {type is type(type)}")

import dis
def check_type():
    return type(42)
print("\ntype() call bytecode:")
dis.dis(check_type)

type and object have a circular relationship: type is a subclass of object, but object is an instance of type. This bootstrap is hard-coded in CPython's C implementation and cannot be expressed in pure Python.

Want to try these examples interactively?

Open Expert Playground