Python type() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

Definition

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.

Syntax

One argument to inspect, three to create a class.

python
cls = type(obj)
NewClass = type(name, bases, namespace)
CallWhat it does
type(3.5)Returns float.
type(x).__name__The class name, e.g. 'list'.
type(x) is intExact type check (no subclasses).
type("A", (), {})Creates a new empty class named A.

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

ParameterDescription
objectOne-argument form: any object whose class you want.
nameThree-argument form: the new class name (str).
basesThree-argument form: tuple of base classes (empty means object).
dictThree-argument form: attributes and methods of the new class.

Return Value

Return ValueDescription
typeThe 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)

How It Works

  1. Class objects. type(value) returns the class itself, such as int or str.
  2. Readable names. .__name__ gives the class name without the <class ...> wrapper.
  3. None has a type. None is the single instance of NoneType.
  4. 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__)

How It Works

  1. Exact class. type(rex) is Dog, not its parent.
  2. Subclasses count for isinstance. A Dog is an Animal, so isinstance() says True.
  3. bool is a subclass of int. True has type bool, but it is still an instance of int.
  4. 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__)

How It Works

  1. Three arguments. The name, a tuple of base classes, and a dict of class attributes.
  2. Functions become methods. greet in the dict turns into a method that receives self.
  3. Same as class syntax. The result behaves exactly as if you had written class Person: by hand.
  4. 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.

Next: vars()

Get the __dict__ of an object, module, or class.

vars() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful