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
| Parameter | Description |
|---|---|
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
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
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
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
isinstance()· Return True if an object is an instance of a class, one of its subclasses, or any class in a tuple or union of classes.issubclass()· Return True if a class is a subclass of another class (or of any class in a tuple or union). A class is a subclass of itself.object()· Return a new featureless object. object is the base class of every Python class and provides the default methods.- Python Data Types · tutorial