w3resource

Python: isinstance() function

isinstance() function

The isinstance() function returns true if the object argument is an instance of the classinfo argument, or of a subclass thereof.
If object is not an object of the given type, the function always returns false. If classinfo is a tuple of type objects return true if object is an instance of any of the types.
If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised.

Version:

(Python 3.2.5)

Syntax:

isinstance(object, classinfo)

Parameter:

Name Description Required /
Optional
object An object. Required
classinfo

A type or a class, or a tuple of types and/or classes.

Optional

Example: Python isinstance() function

num = [2, 4, 6]

x = isinstance(num, list)
print(num,'Instance of list?', x)

x = isinstance(num, dict)
print(num,'Instance of dict?', x)

x = isinstance(num, (dict, list))
print(num,'Instance of dict or list?', x)

number = 7

x = isinstance(num, list)
print(num,'Instance of list?', x)

x = isinstance(num, int)
print(num,'Instance of int?', x)

Output:

[2, 4, 6] instance of list? True
[2, 4, 6] instance of dict? False
[2, 4, 6] instance of dict or list? True
[2, 4, 6] instance of list? True
[2, 4, 6] instance of int? False

Python Code Editor:

Previous: int()
Next: issubclass()

Test your Python skills with w3resource's quiz



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python/built-in-function/isinstance.php