isinstance()

isinstance()

isinstance(object, classinfo)

isinstance() Parameters

The isinstance() takes two parameters:

  • object - object to be checked

  • classinfo - class, type, or tuple of classes and types

Return Value from isinstance()

The isinstance() returns:

  • True if the object is an instance or subclass of a class, or any element of the tuple

  • False otherwise

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

class Foo:
  a = 5
  
fooInstance = Foo()

print(isinstance(fooInstance, Foo))
print(isinstance(fooInstance, (list, tuple)))
print(isinstance(fooInstance, (list, tuple, Foo)))

Output:

True
False
True

References

https://www.programiz.com/python-programming/methods/built-in/isinstance

Last updated