Python
Programming
Type Function
IsInstance Function
Code Comparison

What are the differences between type() and isinstance()?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

type() and isinstance() are both related to type inspection in Python, but they answer different questions. type(obj) tells you the object's exact runtime class, while isinstance(obj, SomeClass) asks whether the object should be treated as an instance of a class or any of its subclasses.

Exact Type Versus Class Membership

The simplest comparison looks like this:

python
1value = 5
2
3print(type(value))                # <class 'int'>
4print(type(value) is int)         # True
5print(isinstance(value, int))     # True

For basic built-in values, the results often look equivalent. The difference becomes important when inheritance enters the picture.

isinstance() Understands Inheritance

Suppose you have a base class and a subclass:

python
1class Animal:
2    pass
3
4class Dog(Animal):
5    pass
6
7pet = Dog()
8
9print(type(pet) is Animal)        # False
10print(isinstance(pet, Animal))    # True

type(pet) is Animal is false because the exact class is Dog. isinstance(pet, Animal) is true because Dog inherits from Animal.

This is why isinstance() is usually the better tool for application logic. It respects polymorphism.

Why type() Is Still Useful

type() is not wrong; it is just stricter. It is useful when you genuinely care about the exact runtime class.

For example:

python
1value = True
2
3print(type(value) is bool)        # True
4print(isinstance(value, int))     # True

That result surprises many people. In Python, bool is a subclass of int. If you want to distinguish True from normal integers, type() may be the clearer choice.

type() is also helpful for debugging:

python
data = {"name": "Ada"}
print(type(data))

Sometimes you just want to know what object class you are dealing with.

isinstance() Can Check Multiple Types

Another advantage of isinstance() is that it accepts a tuple of allowed types:

python
1def normalize_number(value):
2    if isinstance(value, (int, float)):
3        return float(value)
4    raise TypeError("Expected int or float")
5
6print(normalize_number(4))
7print(normalize_number(2.5))

That is a concise way to express “accept any of these compatible types.”

There is no equally clean equivalent with direct type() equality checks.

Practical Rule of Thumb

Use isinstance() when your code is deciding whether an object can be treated like a member of some class family. That is the common case in object-oriented Python.

Use type() when you explicitly need exact type identity or when you are introspecting objects during debugging and diagnostics.

In other words:

  • behavior-oriented checks usually favor isinstance()
  • exact identity checks usually favor type()

An Example with Custom Processing

python
1class Shape:
2    def area(self):
3        raise NotImplementedError
4
5class Circle(Shape):
6    def __init__(self, radius):
7        self.radius = radius
8
9    def area(self):
10        return 3.14159 * self.radius * self.radius
11
12def print_area(obj):
13    if isinstance(obj, Shape):
14        print(obj.area())
15    else:
16        raise TypeError("Expected a Shape")
17
18print_area(Circle(2))

This code works naturally for any future subclass of Shape. If it used type(obj) is Shape, that flexibility would be lost.

Common Pitfalls

The most common mistake is using type(obj) == SomeClass when the code really means “this object can behave like SomeClass.” That accidentally rejects subclasses and makes code less extensible.

Another issue is assuming isinstance() means exact type equality. It does not. It includes inheritance.

A third surprise is special relationships in built-in types, such as bool being a subclass of int. If that distinction matters, exact type checks may be appropriate.

Summary

  • 'type(obj) returns the object's exact runtime class.'
  • 'isinstance(obj, Class) checks class membership and inheritance.'
  • 'isinstance() is usually better for application logic and polymorphism.'
  • 'type() is useful for exact identity checks and debugging.'
  • The two functions are related, but they answer different questions.

Course illustration
Course illustration

All Rights Reserved.