Usage
The isinstance()
function in Python is a built-in function primarily used to check if an object is an instance of a particular class or any class derived from it.
By passing a tuple containing multiple classes or types to the isinstance()
function, you can determine if the object is an instance of any of the specified classes or types.
Syntax
isinstance(object, classinfo)
Parameters
Parameter | Condition | Description |
object | Required | The object you want to check the type of. |
classinfo | Required | This can be:
|
Return Value
The isinstance()
function returns a boolean value. It returns True if the given object is an instance of the specified class, or any of its subclasses, and False otherwise.
When the classinfo
parameter receives a tuple containing multiple classes or types, the isinstance()
function returns True if the object is an instance of any class or type within the tuple; otherwise, it returns False.
Examples
Here’s how to use the isinstance()
function:
print(isinstance(42, int)) # True (42 is an integer)
print(isinstance("Hello", str)) # True ("Hello" is a string)
print(isinstance([1, 2, 3], list)) # True ([1, 2, 3] is a list)
print(isinstance(15, float)) # False (15 is not a float)
print(isinstance("World", list)) # False ("World" is not a list)
Checking Against a Custom Class
You can even use isinstance()
to check if an object is an instance of a custom class.
class Car():
pass
my_car = Car()
print(isinstance(my_car, Car)) # True
In this example, isinstance()
returns True because my_car
is an instance of the Car
class.
Using a Tuple for classinfo
You can also pass a tuple to classinfo
to check if the object is an instance of any of the classes or types in that tuple:
print(isinstance(42, (int, float))) # True (42 is an integer)
print(isinstance(3.14, (int, float))) # True (3.14 is a float)
print(isinstance("Hello", (int, float))) # False ("Hello" is neither an integer no a float)
It’s essentially the same as performing multiple individual isinstance()
checks joined by the OR
operator. So, isinstance(x, (A, B, ...))
is equivalent to isinstance(x, A) OR isinstance(x, B) OR ...
.
print(isinstance(42, int) or isinstance(42, float)) # True
print(isinstance(3.14, int) or isinstance(3.14, float)) # True
isinstance() Accounts for Inheritance
The isinstance()
function takes inheritance into account. It checks if an object is an instance of a particular class or any of its subclasses. This means if an object belongs to a derived class (a subclass), isinstance()
will still recognize it as being an instance of the parent class.
Let’s illustrate this with an example:
class Fruit:
pass
class Apple(Fruit):
pass
apple = Apple()
print(isinstance(apple, Apple)) # True (apple is directly an instance of Apple)
print(isinstance(apple, Fruit)) # True (Apple inherits from Fruit)
In this example, isinstance()
returns True in both cases because the apple
object is an instance of the Apple
class, and, importantly, the Apple
class inherits from the Fruit
class.
Another interesting example is that booleans (True and False) in Python are a subclass of integers. Therefore, a boolean object will return True when checked with isinstance()
against both the bool
and int
types.
print(isinstance(True, bool)) # True
print(isinstance(True, int)) # True