Python 3 reference · Built-in Functions

Python type() Function

With one argument, return an object's type. With three arguments, create a new class dynamically.

Python 3 Since 3.0

Syntax

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

Parameters

ParameterDescription
object Any value whose type you want.
name, bases, dict optional The new class's name, base classes and attributes (three-argument form).

Return value

A type (class) object.

Examples

Find a value's typeTry it yourself
print(type(42), type("hi"), type([]))
print(type(3.0).__name__)
print(type(True) is bool)

Output

<class 'int'> <class 'str'> <class 'list'>
float
True
Create a class dynamicallyTry it yourself
Point = type("Point", (), {"x": 0, "y": 0, "describe": lambda self: f"({self.x}, {self.y})"})
p = Point()
print(p.describe(), type(p).__name__)

Output

(0, 0) Point

Good to know

To check a type, prefer isinstance(x, int), which also accepts subclasses. Use type(x) for debugging and when you need the exact class.

Version history

VersionChange
Python 3.0Added