ScanSkill

isinstance

This isinstance() function returns a Boolean stating whether the object is an instance or subclass of another object.

Syntax

isinstance(object, classinfo)

Here,

  • object: Required. An object.
  • classinfo: Required. A class, type, or a tuple containing classes and types

Note: Also returns True if classinfo is a type object and an object is an object of that type or of a (direct, indirect, or virtual) subclass thereof.

If the object is not a class instance or an object of the given type, the function always returns false.

If classinfo is neither a class object nor a type object, it may be a tuple of class or type objects, or may recursively contain other such tuples (other sequence types are not accepted).

If classinfo is not a class, type, or tuple of classes, types, and such tuples, a TypeError exception is raised.

Examples

>>> class Class1:
...     pass
... 
>>> class Class2(Class1):
...     pass
... 
>>> a = Class2()
>>> isinstance(a, Class1)
True
>>> isinstance(Class2, Class1)
False
>>> isinstance(Class2(), Class1)
True
>>> nos = [1, 2, 3, 4]
>>> isinstance(numbers, list)
True
>>> isinstance(numbers, dict)
False
>>> isinstance(numbers, (dict, list))
True