Python type() and isinstance() Methods: Check Object Types

Python is an object-oriented and advanced language that provides different types and objects to store and process data. These objects can be built-in or custom which are created by developers. While working with different objects finding their type is very important to use them properly. Python built-in type() method used to detect and find given object type and isinstance() method is used to check the given type.

type() Method Syntax

type() method is the most popular method used to find and check the given variable of an object type. It only accepts a single parameter where the type of the given parameter object will be returned as a string.

type(o)
  • o is the object we want to find its type. o can be a single object like integer, string, or multi-type objects like an array or list.

Check Single Type Object with type() Method

We can use single type object where a base object type will be returned. Below we will test the types integer, string, class etc.

a = 12

b = "Wisetut"

class c:
   x="Test"

d= c()

type(a)
#<class 'int'>

type(b)
#<class 'str'>

type(c)
#<class 'type'>

type(d)
<class '__main__.c'>
Check Single Type Object with type() Method

isinstance() Method

isinstance() method is another way to check the given object or variable type. isinstance() also checks if the given object is an instance of the given class and subclass which is more reliable. But in order to check and compare the class name should be provided. The isinstance() method will return the check result. It will return true if the given object type is the same as the type parameter and return false if not the same.

isinstance(o,type)
  • o is the object or variable we want to check. It will be compared with the type parameter.
  • type is a type like an int, string, class, etc which will be checked for o object. Multiple types can be also provided to match one of them like (list, tuple, string).

Lets start with a simple example where we will check some basic and class types.

a = 1 
b = "Wise"
class c:
   x=1

d = c()
e = [1,2,3,4]

isinstance(a,int)

isinstance(b,str)

isinstance(d,c)

isinstance(e,list)
isinstance() Method

If we want to check multiple types with the given object we will provide the types as a tuple. In the following example, we will check the d object for class c, int, and list. The result will be true because the d object is created from class c.

class c:
   x=1

d = c()

isinstance(d,(c,int,list))
//True

isinstance() Method Errors

isinstance() method is used with just objects or variables there is no need to use extra method like type() . If you look following example we will create an empty dictionary and compare it with the dict type. If the type() method is used to compare them it will return false value.

x = {}

isinstance(x,type(dict))
# Returns False

isinstance(x,dict)
# Returns True

Check Class and Subclass Type with issubclass() Method

Even the isinstance() method provides a generic way to check diferent object types there is a method named issubclass() which is specifically used to check if given object or class is subclass of the specified class or object. the issubclass() method accepts two parameters and its syntax is like below.

issubclass( PARENT , CHILD)
  • PARENT is the parent class which will be checked if the CHILD is its subclass.
  • CHILD is derived or inherted class from the PARENT class.

If the CHILD class is derived or inherited from the PARENT class the issubclass() method will return true if not it will return false.

class Human:
   age = 0

class Boy(Human):
   name = ""

class Animal():
   length=0


issubclass(Human,Boy)
# Output is true

issubclass(Boy,Human)
# Output is false

issubclass(Human,Animal)
# Output is false

Leave a Comment