type()

type(object) / type(name, bases, dict)
Returns: type object · Updated March 13, 2026 · Built-in Functions
built-in types introspection

The type() function has two distinct uses. When called with a single argument, it returns the type of that object. When called with three arguments, it creates a new type object dynamically, which is the mechanism behind class definitions.

Syntax

type(object)
type(name, bases, dict)

Parameters

ParameterTypeDefaultDescription
objectanyAny Python object — type() returns its type
namestrThe name of the new type (used with 3-argument form)
basestupleTuple of base classes (used with 3-argument form)
dictdictDictionary containing attributes and methods (used with 3-argument form)

Examples

Getting the type of an object

x = 42
print(type(x))
# <class 'int'>

name = "Alice"
print(type(name))
# <class 'str'>

items = [1, 2, 3]
print(type(items))
# <class 'list'>

Comparing types

value = "hello"

# Using type() directly
if type(value) is str:
    print("It's a string")
# It's a string

# More common: isinstance()
if isinstance(value, str):
    print("It's a string (using isinstance)")
# It's a string (using isinstance)

Creating types dynamically (3-argument form)

# Create a new class dynamically
MyClass = type("MyClass", (object,), {"x": 42, "greet": lambda self: "Hello"})

instance = MyClass()
print(instance.x)
# 42
print(instance.greet())
# Hello

Common Patterns

Type checking

type() works but isinstance() is preferred:

def process(value):
    # Works but limited
    if type(value) is list:
        return sum(value)
    
    # Better — handles subclasses
    if isinstance(value, (list, tuple)):
        return sum(value)

Dynamic class creation

# Factory function for creating classes
def create_class(class_name, attributes):
    return type(class_name, (object,), attributes)

Person = create_class("Person", {"name": "Unknown", "age": 0})
p = Person()
print(p.name)
# Unknown

Inspecting unknown objects

def describe(value):
    print(f"Type: {type(value).__name__}")
    print(f"Module: {type(value).__module__}")

describe(42)
# Type: int
# Module: builtins

See Also