In Python, type() and isinstance() are two built-in functions used to determine the type of an object. Although these functions seem similar, they have some differences that are important to understand when choosing which one to use.
Here are some key difference between type() and isinstance()
The definition of type()
type() returns the type of an object.
For example
>>> type(3)
<class 'int'>
>>> type("Hello")
<class 'str'>
>>> type([1, 2, 3])
<class 'list'>
The definition of isinstance()
isinstance() checks if an object is an instance of a specified class or of a subclass of that class.
isinstance() returns a boolean value, True if the object is an instance of the specified class or of a subclass, and False otherwise.
For example
>>> isinstance(3, int)
True
>>> isinstance("Hello", str)
True
>>> isinstance([1, 2, 3], list)
True
>>> isinstance(3, float)
False
Note: That isinstance() can also accept a tuple of classes as its second argument, allowing you to check if an object is an instance of any of the classes in the tuple.
Syntax of type() & instance()
- type(object)
- isinstance(object, classinfo)
Usage of type() & instance()
- type() is used to get the type of an object, regardless of its inheritance.
- isinstance() is used to check the inheritance of an object.
In addition to the differences between type() and isinstance() described in my previous answer, it’s also worth noting that type() is a low-level function that only returns the type of an object, while isinstance() provides a more high-level and flexible way of checking the type of an object.
For example, if you have a class hierarchy in which you have a base class and multiple subclasses, you can use isinstance() to check if an object is an instance of the base class or any of its subclasses, without having to manually check the type of the object against each subclass.
class BaseClass:
pass
class SubClass1(BaseClass):
pass
class SubClass2(BaseClass):
pass
Example:
>>> obj = SubClass1()
>>> isinstance(obj, BaseClass)
True
>>> isinstance(obj, (BaseClass, SubClass2))
True
>>> isinstance(obj, SubClass2)
False
Another advantage of isinstance() is that it allows you to check the type of an object against a tuple of types, which is more flexible than using type().
>>> isinstance("hello", (int, float, str))
True
>>> type("hello") == str
True
So, while both type() and isinstance() are useful functions for determining the type of an object in Python, isinstance() provides a more flexible and high-level approach to type checking.
In summary, if you want to check the exact type of an object, use the type() function. If you want to check if an object is an instance of a specific class or a subclass of that class, use the isinstance() function.
In conclusion, both type() and isinstance() functions are useful when determining the type of an object in Python, but they serve different purposes. type() returns the type of an object, while isinstance() checks if an object is an instance of a specified class or a subclass of that class.
Choose the right function based on your requirement and avoid using both functions interchangeably. Thus the above-mentioned points differentiate type() and isinstance() in python.
For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions