> ## 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.

# Python type

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.

```python theme={"dark"}
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.

```python theme={"dark"}
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.

```python theme={"dark"}
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.

```python theme={"dark"}
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`.

```python theme={"dark"}

# 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.

```python theme={"dark"}
class CustomMeta(type):
    def __new__(cls, name, bases, dct, **kwargs):
        # Intercept and modify class creation here
        return super().__new__(cls, name, bases, dct, **kwargs)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
