# The fundamental bootstrap:# - type is an instance of itself# - type is a subclass of object# - object is an instance of typeprint(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 typeprint(f"type.__mro__: {type.__mro__}")
# This circular relationship is bootstrapped in C code# and cannot be recreated in pure Pythonprint(f"\ntype is type(type): {type is type(type)}")
import dis
defcheck_type():
returntype(42)
print("\ntype() call bytecode:")
dis.dis(check_type)
Output
Click "Run" to execute your code
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.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?