🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

type()

AI & DATA SCIENCE // type

type() returns the class (type) of an object when called with one argument, or dynamically creates a brand-new class when called with three.

Syntax

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

Deep Dive Course

The single-argument form, type(obj), is the everyday use: it returns the exact class an object belongs to, e.g. type(5) is int. Every object in Python has a type, including classes themselves. The rarely-used three-argument form dynamically builds a new class at runtime — it's what's happening behind the scenes whenever you write a class statement, since a plain class definition is roughly sugar for calling type() directly.

1Understanding type()

The single-argument form, type(obj), is the everyday use: it returns the exact class an object belongs to, e.g. type(5) is int. Every object in Python has a type, including classes themselves. The rarely-used three-argument form dynamically builds a new class at runtime — it's what's happening behind the scenes whenever you write a class statement, since a plain class definition is roughly sugar for calling type() directly.

💡

Prefer isinstance() over comparing type(x) to a class directly — isinstance() also matches subclasses, which is almost always what you actually want.

editor.html
print(type(42))
print(type("hello"))
print(type([1, 2, 3]))
localhost:3000

2Practical Example

Here is a real-world application of type() showing how it is used in production Python code.

editor.html
# Dynamically creating a class, equivalent to "class Point: pass"
Point = type("Point", (), {"x": 0, "y": 0})
p = Point()
print(p.x, p.y)
localhost:3000

3Best Practices

Follow these guidelines when working with type():

1. Use isinstance() for type checks in conditionals; reserve type() for introspection/debugging

2. Use type(obj).__name__ to get a readable class name as a string

3. Avoid the 3-argument dynamic class creation unless you're writing a metaprogramming library

⚠️

Tip: Prefer isinstance() over comparing type(x) to a class directly — isinstance() also matches subclasses, which is almost always what you actually want.

editor.html
print(type(42))
print(type("hello"))
print(type([1, 2, 3]))
localhost:3000

Examples

Example 01Basic Usage
print(type(42))
print(type("hello"))
print(type([1, 2, 3]))
Example 02Advanced Example
# Dynamically creating a class, equivalent to "class Point: pass"
Point = type("Point", (), {"x": 0, "y": 0})
p = Point()
print(p.x, p.y)

Best Practices

  • Use isinstance() for type checks in conditionals; reserve type() for introspection/debugging
  • Use type(obj).__name__ to get a readable class name as a string
  • Avoid the 3-argument dynamic class creation unless you're writing a metaprogramming library

Interview Question

What's the difference between checking type(x) directly against a class and using isinstance(x, cls)?

Hint: Think about subclassing.

Comparing type(x) directly only matches objects whose type is exactly that class, rejecting subclasses. isinstance(x, cls) returns True for cls itself and for any subclass of it, which respects polymorphism and is the recommended way to check types in almost all real code.

Exercises

MediumPractice using type() in a real scenario.
View Solution
print(type(42))
print(type("hello"))
print(type([1, 2, 3]))

Frequently Asked Questions

What's the difference between checking type(x) directly against a class and using isinstance(x, cls)?

Comparing type(x) directly only matches objects whose type is exactly that class, rejecting subclasses. isinstance(x, cls) returns True for cls itself and for any subclass of it, which respects polymorphism and is the recommended way to check types in almost all real code.

Related Functions

isinstance()classid()