Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

In Python, type is a built-in metaclass and function that serves a dual purpose: it returns the exact type of an existing object when invoked with a single positional argument, and it dynamically constructs a new class object when invoked with exactly three positional arguments (alongside optional keyword arguments). It is the root metaclass in Python’s object model, meaning all classes—including built-ins, user-defined classes, and type itself—are instances of type.

Single-Argument Syntax: Type Inspection

When passed exactly one argument, type() returns the class object that instantiated the argument.
type(object)
  • object: Any Python object.
  • Return Value: The class object of the argument.
Unlike accessing the __class__ attribute, type() always reads the underlying C-level type pointer directly (such as ob_type in CPython) to determine the true type. Consequently, type() bypasses any Python-level interception of __class__. This includes custom descriptors (e.g., @property), overriding __getattribute__, or shadowing the attribute with a regular class variable. However, if __class__ is dynamically reassigned on an instance (e.g., x.__class__ = NewClass), Python updates the underlying ob_type pointer at the C level. In this specific scenario, type(x) will accurately reflect the newly assigned class because the internal pointer itself was modified.
class Deceptive:
    # Shadowing __class__ with a class variable
    __class__ = int

    def __getattribute__(self, name):
        # Intercepting attribute access
        if name == '__class__':
            return str
        return super().__getattribute__(name)

x = Deceptive()


# type() directly reads the C-level pointer, bypassing all Python-level interception
print(type(x))       # <class '__main__.Deceptive'>
print(x.__class__)   # <class 'str'>

Dynamic Class Creation

When passed exactly three positional arguments, type() acts as a class constructor (metaclass), allocating memory and initializing a new class object at runtime. Passing two, or four or more positional arguments raises a TypeError. This three-argument form is the underlying mechanism Python uses when it parses a class statement.
type(name, bases, dict, **kwds)
  • name (str): The name of the class. Becomes the __name__ attribute.
  • bases (tuple): A tuple of parent classes for inheritance. Becomes the __bases__ attribute.
  • dict (dict): A dictionary containing the namespace (attributes and methods) for the class body. This dictionary is used to populate the class namespace. The resulting class’s __dict__ attribute is a distinct, read-only mappingproxy object, not a reference to the original dictionary passed to the constructor.
  • **kwds: Optional keyword arguments that are passed to the __init_subclass__ method of the base classes or to custom metaclasses.
class BaseClass:
    pass


# Static class definition
class MyClass(BaseClass):
    x = 5
    def echo(self):
        return self.x


# Equivalent dynamic class creation using type()
MyClassDynamic = type(
    'MyClass', 
    (BaseClass,), 
    {
        'x': 5, 
        'echo': lambda self: self.x
    }
)

The Metaclass Hierarchy

In Python’s type system, there is a strict separation between the inheritance hierarchy and the instantiation hierarchy.
  • object is the root of the inheritance hierarchy. All classes inherit from object.
  • type is the root of the instantiation hierarchy. All classes are instances of type.
Because type is itself a class, it is an instance of itself, and because it is a class, it inherits from object.

# Instantiation hierarchy (what created it?)
print(type(int))        # <class 'type'>
print(type(object))     # <class 'type'>
print(type(type))       # <class 'type'>


# Inheritance hierarchy (what does it inherit from?)
print(issubclass(int, object))   # True
print(issubclass(type, object))  # True
When a custom metaclass is defined, it must inherit from type to properly hook into Python’s class creation mechanics.
class CustomMeta(type):
    def __new__(cls, name, bases, dct, **kwargs):
        # Intercept and modify class creation here
        return super().__new__(cls, name, bases, dct, **kwargs)
Master Python with Deep Grasping Methodology!Learn More