isinstance() method - Python
isinstance() is a built-in Python function that checks whether an object or variable is an instance of a specified type or class (or a tuple of classes), returning True if it matches and False otherwise, making it useful for type-checking and ensuring safe operations in dynamic code. Example:
x = 10
print(isinstance(x, int)) # x is int
class Animal: pass
dog = Animal()
print(isinstance(dog, Animal)) # dog is Animal
Output
True True
Explanation:
- isinstance(x, int) returns True as x is an integer.
- isinstance(dog, Animal) returns True as dog is an instance of the Animal class.
Syntax of isinstance()
isinstance(obj, classinfo)
Parameters:
- obj: The object to check.
- classinfo: A class, type or a tuple of classes/types.
Returns:
- True if obj is an instance of classinfo or any type in the tuple and otherwise False.
Raises: TypeError if classinfo is not a valid class or type.
Examples of isinstance()
Example 1: In this example, we use isinstance() to check if an integer and a list are instances of the int or str types.
a = 5
b = [10,20,37]
print(isinstance(a, int))
print(isinstance(b, list))
print(isinstance(b, (int, list,str)))
Output
True True True
Explanation:
- isinstance(a, int) returns True because a is an integer.
- isinstance(b, list) returns True because b is a list.
- isinstance(b, (int, list, str)) returns True because b is a list, which is one of the types in the tuple.
Example 2: In this example, we check if an object is an instance of a specific class or its parent class.
class Animal: pass
class Dog(Animal): pass
dog = Dog()
print(isinstance(dog, Dog))
print(isinstance(dog, Animal))
Output
True True
Explanation:
- isinstance(dog, Dog) returns True as dog is an instance of the Dog class.
- isinstance(dog, Animal) returns True as dog is also an instance of the parent class Animal.
Example 3: In this example, we check if an object is an instance of a specific type, such as a Python string or a dictionary.
a = "Hello"
print(isinstance(a, str))
b = {"apple": 1}
print(isinstance(b, dict))
Output
True True
Explanation:
- isinstance(a, str) returns True as a is a string.
- isinstance(b, dict) returns True as b is a dictionary.
Example 4: In this example, we check if an object is an instance of a class or its derived class.
class MyClass:
def method(self): return "Hello"
obj = MyClass()
print(isinstance(obj.method(), str))
Output
True
Explanation: isinstance(obj.method(), str) returns True as obj.method() returns the string "Hello".
Difference between isinstance() and type() methods in Python
The following table highlights the differences between the isinstance() and type() methods, both used for type checking. Knowing when to use each helps write more efficient and reliable code.
isinstance() | type() |
---|---|
Syntax: isinstance(object, class) | Syntax: type(object) |
It checks if an object is of a specific class type | It returns the class type of an object |
It can check if the object belongs to a class and its subclasses | It cannot deal with inheritance |
It is faster as compared to type() | It is slower than isinstance() |
It returns either True or False | It returns the type of the object |
It can check for multiple classes at a time | It cannot do this |
Example: isinstance(10, (int, str)) | Example: type(10) |