Latest Code Tutorials

isinstance() Function in Python with Example

The isinstance() function checks if the object which is the first argument is an instance or subclass of classinfo class, which is the second argument.

Python isinstance()

Python isinstance() is an inbuilt method that returns True if a specified object is of the specified type. Otherwise, it returns False. If the type parameter is the tuple, this function will return True if the object is one of the types in the tuple.


The syntax of the Python isinstance() method is the following.

isinstance(object, classinfo)


The object parameter is required, and it is an object that needs to be checked.

A classinfo or a class, or a tuple of types and classes.

If classinfo is not a type or tuple of types, a TypeError exception is raised.

See the following example.


class App:
  GoT = 'Aegon'
series = App()

print(isinstance(series, App))
print(isinstance(series, (list, tuple)))
print(isinstance(series, (list, tuple, App)))

The output of the above code is the following.

Python IsInstance Example

Let’s see other examples.


numbers = [19, 21, 46]

result = isinstance(numbers, list)
print(numbers,'instance of list?', result)

result = isinstance(numbers, dict)
print(numbers,'instance of dict?', result)

result = isinstance(numbers, (dict, list))
print(numbers,'instance of dict or list?', result)

number = 21

result = isinstance(number, list)
print(number,'instance of list?', result)

result = isinstance(number, int)
print(number,'instance of int?', result)

The output is the following.

Python IsInstance Tutorial Example

Check if “AppDividend” is one of the types described in the type parameter.


xyz = isinstance("AppDividend", (float, int, str, list, dict, tuple))

The output is the following.

Python isinstance() Function Tutorial

#type() in Python

Python has a built-in method called a kind which generally comes in handy while figuring out the type of the variable used in the program in the runtime.

If a single argument (object) is passed to type() built-in, it returns the type of the given object.

If three arguments (name, bases, and dict) are passed, it returns a new type object. See the following example of the type() function.


a = 10
b = "AppDividend"
c = [19,21,46] 

The output is the following.

type in Python

#More Examples

Check if “AppDividend” is one of the types described in the type parameter.


x = isinstance("AppDividend", (float, int, str, list, dict, tuple))

The AppDividend value is a type of String, which is included in the second parameter. So it will return True. Otherwise, it would have return False. The other types are float, int, list, dict, and tuple.

#type() vs. isinstance()

Python type() returns the type object of an object, and comparing what it returns to another type object will only yield True when you use the same type object on both sides.

Python isinstance() will help you check to see if an object has a certain type.

In Python, it’s preferable to use Duck Typing( type checking be deferred to run-time and is implemented using dynamic typing or reflection) rather than inspecting the type of an object.

Conclusively, the Python isinstance function is over.

Recommended Posts

Python callable()

Python chr()

Python classmethod()

Python bool()

Python bin()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.