🚀 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...

isinstance()

AI & DATA SCIENCE // isinstance

isinstance() checks whether an object is an instance of a given class or type, or of any class in a tuple of types — including subclasses.

Syntax

isinstance(object, classinfo)

Deep Dive Course

isinstance(obj, cls) returns True if obj's type is cls or any subclass of cls, which is why it's the recommended way to check types in Python instead of comparing type(obj) directly, since that comparison rejects subclasses. Passing a tuple as the second argument checks against multiple types at once, matching if obj is an instance of any of them.

1Understanding isinstance()

isinstance(obj, cls) returns True if obj's type is cls or any subclass of cls, which is why it's the recommended way to check types in Python instead of comparing type(obj) directly, since that comparison rejects subclasses. Passing a tuple as the second argument checks against multiple types at once, matching if obj is an instance of any of them.

💡

Remember bool is technically a subclass of int in Python, so a boolean value also counts as an instance of int, which occasionally causes surprising bugs when checking for 'any number'.

editor.html
print(isinstance(5, int))
print(isinstance("hi", (int, str)))
print(isinstance(5, str))
localhost:3000

2Practical Example

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

editor.html
def describe(value):
    if isinstance(value, bool):
        return "boolean"
    if isinstance(value, (int, float)):
        return "number"
    return "other"

print(describe(True))
print(describe(3.14))
localhost:3000

3Best Practices

Follow these guidelines when working with isinstance():

1. Use isinstance() rather than direct type comparisons so subclasses are correctly recognized

2. Pass a tuple of types to check for several acceptable types in one call, instead of chaining multiple checks with or

3. Guard against bool being a subclass of int explicitly if that distinction matters for your logic

⚠️

Tip: Remember bool is technically a subclass of int in Python, so a boolean value also counts as an instance of int, which occasionally causes surprising bugs when checking for 'any number'.

editor.html
print(isinstance(5, int))
print(isinstance("hi", (int, str)))
print(isinstance(5, str))
localhost:3000

Examples

Example 01Basic Usage
print(isinstance(5, int))
print(isinstance("hi", (int, str)))
print(isinstance(5, str))
Example 02Advanced Example
def describe(value):
    if isinstance(value, bool):
        return "boolean"
    if isinstance(value, (int, float)):
        return "number"
    return "other"

print(describe(True))
print(describe(3.14))

Best Practices

  • Use isinstance() rather than direct type comparisons so subclasses are correctly recognized
  • Pass a tuple of types to check for several acceptable types in one call, instead of chaining multiple checks with or
  • Guard against bool being a subclass of int explicitly if that distinction matters for your logic

Interview Question

Why does checking isinstance() of a boolean against int return True, and how would you write a check that treats booleans as their own type?

Hint: bool has a special relationship to int in Python's type hierarchy.

In Python, bool is implemented as a subclass of int — True and False behave as 1 and 0 in arithmetic — so isinstance() correctly reports a boolean as also being an int, since subclasses satisfy isinstance checks for their parent class. To exclude booleans specifically, check that the value is an instance of int and also confirm it is not an instance of bool.

Exercises

MediumPractice using isinstance() in a real scenario.
View Solution
print(isinstance(5, int))
print(isinstance("hi", (int, str)))
print(isinstance(5, str))

Frequently Asked Questions

Why does checking isinstance() of a boolean against int return True, and how would you write a check that treats booleans as their own type?

In Python, bool is implemented as a subclass of int — True and False behave as 1 and 0 in arithmetic — so isinstance() correctly reports a boolean as also being an int, since subclasses satisfy isinstance checks for their parent class. To exclude booleans specifically, check that the value is an instance of int and also confirm it is not an instance of bool.

Related Functions

type()issubclass()class