Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

isinstance

Tests whether an object is an instance of a type, accepted subclass, tuple of types, or Union Type. It returns a Boolean and supports subclass-aware checks.

Since
3.10 for Union Type support

How it works

isinstance() tests whether an object belongs to a specified type, any type in a tuple, or a type related through direct, indirect, or virtual subclassing. It returns a Boolean result; an invalid classinfo argument raises TypeError, subject to earlier successful checks.

Syntax

isinstance(object, classinfo, /)

Parameters

NameTypeDescription
objectobjectThe value whose instance or subclass relationship is tested.
classinfotype | tuple[type, ...] | union typeA type, recursively nested tuple of types, or a Union Type of multiple types.

Returns

True if object is an instance of classinfo or one of its accepted subclasses; otherwise False.

Key points

  • The function returns True when the object is an instance of the supplied classinfo.
  • Subclass relationships count, including direct, indirect, and virtual subclasses.
  • A nonmatching object produces False.
  • A tuple can contain types and nested tuples of types.
  • A union type can represent multiple accepted types.
  • For multiple candidate types, one matching type is sufficient.
  • Union Type support was added in Python 3.10.

Common mistakes

  • classinfo must be a type, a recursively nested tuple of types, or a supported union type; otherwise the call can raise TypeError.
  • With a tuple of candidate types, an earlier successful match can prevent an invalid later entry from raising TypeError.

Example: Checking one or several types

value = 42

if isinstance(value, int):
    print("integer")

if isinstance(value, (str, float)):
    print("text or decimal")

The first test succeeds because 42 is an int. The second test checks several acceptable types and therefore does not print anything.

When to use it

Input validation

Validate an input before applying type-specific operations.

Multiple allowed types

Accept several alternative representations in one conditional.

Subclass-aware dispatch

Recognize derived or virtual implementations through a base type.

Interview questions

QWhat does isinstance() return?

isinstance(value, Type) returns True when value is an instance of Type or of a direct, indirect, or virtual subclass. Otherwise it returns False.

Follow-up: How can one test several types? Pass a tuple of type objects, such as isinstance(value, (int, float)); the result is true if any candidate matches.

QWhat forms can classinfo take?

The second argument may be a type, a recursively nested tuple of type objects, or a union type. For a tuple, the function succeeds when the object matches at least one contained type.

Follow-up: What happens if classinfo is invalid? A TypeError is raised, although an earlier successful tuple check can prevent that error.

QWhy use isinstance() rather than checking an object's exact type?

isinstance() accounts for subclass relationships, including direct, indirect, and virtual subclasses. This makes it suitable when accepted derived types should pass a type check.

Follow-up: When was union-type support added? The documentation records that classinfo can be a Union Type starting in Python 3.10.

  • issubclass
  • type
  • Union Type

Source: Built-in functions. Checked 2026-10-15.