In Python, type(obj) returns the class of an object: type(42) is int and type("hi") is str. Use type(obj).__name__ to get the class name as a string.
For checks, isinstance() is usually better, because it also accepts subclasses. The rarely used three-argument form type(name, bases, dict)creates a new class at runtime, exactly like a class statement does.
type is a built-in type (a class) that you call like a function: each call creates a new type object. Like every built-in, it lives in the builtins module and is always available without an import.
Printing a class shows <class 'int'>; .__name__ gives just int.
Library declarations
python
type(object, /)
type(name, bases, dict, /, **kwds)
# class type: the metaclass of all classes
Parameters
Parameter
Description
object
One-argument form: any object whose class you want.
name
Three-argument form: the new class name (str).
bases
Three-argument form: tuple of base classes (empty means object).
dict
Three-argument form: attributes and methods of the new class.
Return Value
Return Value
Description
type
The object's class, or the newly created class.
Python Examples
Example 1 — Find the Type of Common Values
Print the class name of several values and compare a type directly.
python
for value in [42, 3.14, "hi", [1], None, True]:
print(repr(value), "->", type(value).__name__)
print(type(42) is int)
Output
42 -> int
3.14 -> float
'hi' -> str
[1] -> list
None -> NoneType
True -> bool
True
How It Works
Class objects.type(value) returns the class itself, such as int or str.
Readable names..__name__ gives the class name without the <class ...> wrapper.
None has a type.None is the single instance of NoneType.
Identity check. Classes are unique objects, so type(42) is int is True.
Example 2 — type() Ignores Inheritance, isinstance() Does Not
Compare both checks on a subclass instance and on True.
python
class Animal:
pass
class Dog(Animal):
pass
rex = Dog()
print(type(rex).__name__)
print(type(rex) is Animal, isinstance(rex, Animal))
print(type(True) is int, isinstance(True, int))
print(type(type(rex)).__name__)
Output
Dog
False True
False True
type
How It Works
Exact class.type(rex) is Dog, not its parent.
Subclasses count for isinstance. A Dog is an Animal, so isinstance() says True.
bool is a subclass of int.True has type bool, but it is still an instance of int.
Classes have a type too. The type of a class is type, the default metaclass.
Example 3 — Create a Class with type(name, bases, dict)
Build a class at runtime with an attribute and a method, then use it like a normal class.
python
def greet(self):
return "Hi, I am " + self.name
Person = type("Person", (object,), {"species": "human", "greet": greet})
p = Person()
p.name = "Ana"
print(Person.__name__, Person.species)
print(p.greet())
print(isinstance(p, Person), Person.__bases__[0].__name__)
Output
Person human
Hi, I am Ana
True object
How It Works
Three arguments. The name, a tuple of base classes, and a dict of class attributes.
Functions become methods.greet in the dict turns into a method that receives self.
Same as class syntax. The result behaves exactly as if you had written class Person: by hand.
Base recorded.__bases__ holds the parent classes you passed.
Frequently Asked Questions
Use type(x) to see its class, or isinstance(x, SomeType) to test it. type(x).__name__ gives the name as a string.
Use isinstance() for checks, because it respects inheritance. Use type() when you need the exact class or its name.
It creates a new class dynamically, equivalent to a class statement with that name, those bases, and those attributes.
True is a bool, and bool is a subclass of int. type() reports the exact class; isinstance(True, int) is True.
Usually type itself. type is the metaclass that creates classes.