isinstance Python що це: перевірка типу об’єкта в коді

isinstance in Python: object type checking in code

isinstance in Python is a built-in function for checking whether an object is an instance of a specific class or one of several classes. It is often used when code needs to handle numbers, strings, lists, custom classes, or objects in an inheritance hierarchy differently.

In practice, isinstance() provides a clear way to check a type without complex comparisons and helps catch mistakes before a risky operation runs.

What isinstance() does

isinstance() returns True if an object belongs to the specified class or to any class in a tuple of classes, and False otherwise. That makes it more useful than a simple type comparison when inheritance matters.

The basic syntax is:

isinstance(object, class)

Example:

isinstance(42, int)True

isinstance(“hello”, int)False

Checking multiple types at once

isinstance in Python also works with a tuple of classes, which is useful when one value can legally be one of several types.

Example:

isinstance(value, (int, float))

This returns True for integers and floating-point numbers. It is a common pattern when validating numeric input before calculations.

Why isinstance() is better than type()

isinstance() is usually the better choice because it respects inheritance. A subclass instance will still pass an isinstance() check for its parent class, while type() only matches the exact class.

That difference matters in real code. If a function accepts a base class and any of its subclasses, isinstance() keeps the check flexible without extra logic.

Example with custom classes

isinstance in Python is especially useful with your own classes, where object behavior depends on the class it comes from.

Example:

class Animal:
    pass
class Dog(Animal):
    pass
pet = Dog()
isinstance(pet, Animal)True

Here, Dog inherits from Animal, so the check succeeds even though the object was created from the child class.

Common mistakes

isinstance() is simple, but a few mistakes come up often:

  • Using type() when inheritance should be accepted.
  • Passing a non-class value as the second argument.
  • Forgetting that a tuple of classes can be used for multiple allowed types.
  • Using type checks where duck typing or exception handling would be cleaner.

A quick verification step is to test the function with both an expected value and an unexpected one. If the result does not match your logic, check whether inheritance, tuples of classes, or the argument order is the issue.

When to use isinstance()

isinstance in Python is the right tool when code must branch based on object type and the check should remain readable. It is especially helpful in validation, input handling, and class-based logic.

Use it carefully, though. If a design can rely on polymorphism instead of explicit type checks, that is often the cleaner option. When a type check is necessary, isinstance() is usually the safest and most Pythonic choice.