The main difference between
isinstance() is that
type(object) returns the type of an object and
isinstance(object, class) returns
True if the
object argument is an instance of the
class argument or in a direct or indirect subclass relationship.
To strengthen your understanding, let’s quickly recap the syntactical definitions of both functions:
type(object)– Returns a string representation of the object’s type.
isinstance(object, class)– Returns a Boolean
Trueif the object is an instance of the class, and
The following examples of both functions show the difference when checking whether an object is of the type list:
>>> type([1, 2, 3]) <class 'list'> >>> isinstance([1, 2, 3], list) True
type() to check for the exact type of an object. You’d use
isinstance() to confirm your suspicions regarding the type of an object. The function
isinstance() is more robust in that you don’t rely on tiny string differences, and it also works with type inheritance. For example, if A inherits from B which inherits from C, an object of type C is also an instance of A.
In particular, here’s a list of differences between
- Return value:
type()returns a string whereas
isinstance()returns a Boolean.
type()takes one argument whereas
isinstance()takes two arguments.
type()is commonly used if you don’t know the type of an object whereas
isinstance()is commonly used to confirm your suspicion regarding a type of an object.
type()can be used with one argument or with three arguments to create a new instance of a certain type, whereas
isinstance()must be used with two arguments.
type()can check only one object whereas
isinstance()can check multiple classes for a given instance if you use a tuple of values as a second argument.
type()doesn’t return any information of the subclass relationship whereas
isinstance()works on direct and indirect subclass relations as well.
On the other hand, here’s a list of similarities between
isinstance()are both used to learn more about the type of a given object.
isinstance()are both Python built-in functions that can be used without importing any library.
isinstance()both concern object-oriented programming.
To learn more about object-oriented programming and become a master, check out our Finxter Computer Science Academy course:
You can watch my introductory video on the
type() function here:
And feel free to also watch my introductory video about the
isinstance() function here:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.