Python isinstance() Function

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

Definition

In Python, isinstance(obj, cls) returns True if obj is an instance of cls or of any subclass of it. isinstance(5, int) is True, and so is isinstance(True, int), because bool is a subclass of int.

Pass a tuple of classes to accept any of them: isinstance(x, (int, float)). It also works with abstract base classes from collections.abc, so you can ask β€œis this iterable?” without listing every concrete type. Since Python 3.10 you can also write int | float.

isinstance() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

Pass the object and a class or a tuple of classes.

python
ok = isinstance(obj, SomeClass)
ok = isinstance(obj, (ClassA, ClassB))
CallWhat it does
isinstance(5, int)Returns True.
isinstance(5, (int, float))True if it is an int or a float.
isinstance(dog, Animal)True for subclasses too.
isinstance(x, Iterable)Uses collections.abc.

Check bool before int when the difference matters, because every bool is also an int.

Library declarations
python
isinstance(object, classinfo, /)

Parameters

ParameterDescription
objectRequired. The value to test.
classinfoRequired. A class, a tuple of classes (nested tuples allowed), or on 3.10+ a union like int | str.

Return Value

Return ValueDescription
boolTrue if the object is an instance of the class or a subclass.

Python Examples

Example 1 — Check Numbers and Strings

Test single classes and tuples of classes, including the bool edge case.

python
print(isinstance(5, int))
print(isinstance(5.0, int))
print(isinstance("5", (int, float)))
print(isinstance(5.0, (int, float)))
print(isinstance(True, int))

How It Works

  1. Exact match. 5 is an int.
  2. No conversion. 5.0 is a float, even though its value equals 5.
  3. Tuple of options. "5" is neither an int nor a float; 5.0 matches float.
  4. bool counts as int. bool is a subclass of int, so True passes.

Example 2 — Subclasses and Abstract Base Classes

Check a class hierarchy, then ask whether values are iterable or mappings.

python
from collections.abc import Iterable, Mapping


class Shape:
    pass


class Circle(Shape):
    pass


c = Circle()
print(isinstance(c, Circle), isinstance(c, Shape), isinstance(c, object))
print(isinstance([1, 2], Iterable), isinstance(42, Iterable))
print(isinstance({"a": 1}, Mapping), isinstance([("a", 1)], Mapping))

How It Works

  1. Whole chain. A Circle is a Circle, a Shape, and an object.
  2. Capability checks. Iterable recognizes anything with __iter__, without listing list, tuple, and so on.
  3. Not iterable. An int cannot be looped over.
  4. Mapping vs pairs. A dict is a Mapping; a list of pairs is not, even though dict() accepts one.

Example 3 — Describe a Value by Its Type

Branch on types in the right order so that booleans are not reported as numbers.

python
def describe(value):
    if isinstance(value, bool):
        return "a boolean"
    if isinstance(value, (int, float)):
        return "a number"
    if isinstance(value, str):
        return "text of length " + str(len(value))
    if isinstance(value, (list, tuple)):
        return "a sequence of " + str(len(value)) + " items"
    return "something else: " + type(value).__name__


for item in [True, 7, 2.5, "hey", (1, 2), None]:
    print(describe(item))

How It Works

  1. Order matters. The bool check comes first; otherwise True would match (int, float).
  2. Group similar types. One tuple covers both ints and floats.
  3. Sequences. Lists and tuples share a branch and report their length.
  4. Fallback. Anything else, like None, reports its type name.

Frequently Asked Questions

isinstance(value, str).
Pass a tuple: isinstance(x, (int, float)). On Python 3.10+ you can also write isinstance(x, int | float).
isinstance() accepts subclasses; type(x) is C only matches the exact class.
bool is a subclass of int, so every bool is also an int. Check bool first if you need to tell them apart.
Use isinstance(x, collections.abc.Iterable), or simply try iter(x) and catch TypeError.

Next: issubclass()

Check whether a class inherits from another class.

issubclass() →

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.

8 people found this page helpful