14-5. The isinstance() Function

The isinstance() function in Python is used to check if an object is an instance of a specified class or if it is an instance of a subclass of that class.

Here's the general syntax:

isinstance(object, classinfo)
  • object: The object to be checked
  • classinfo: A class or a tuple of classes to be checked against

 

In the above example, car_instance is an instance of both the Car class and its parent class Vehicle, so isinstance() returns True for both checks.

Output:

True
True