[Python] Check the exact type of an object

Created on November 30, 2026

You can compare the result of type() with a type to check for an exact match.

a = 1
if type(a) == int:
    print("a is an int")
a is an int

The same check also works with a class you define.

class Hello:
    pass


h = Hello()
if type(h) == Hello:
    print("h is a Hello")
h is a Hello

Use isinstance() if instances of subclasses should also match.

Python