Python isinstance() Function
Returns True
if the object is an instance of the specified type, if not
this function returns False.
Syntax
Python
Copy Code
isinstance(object, type) isinstance(object, (type1, type2, ...))
Parameters
Parameter | Description |
---|---|
object |
Required. The object to check |
type |
Required. The class type we to check against object. Either a class or a tuple containing classes must be provided as the second parameter. |
(type1, type2, ...) |
Required.A tuple containing a collection of classes to be checked. Either an object or a tuple containing classes must be provided as the second parameter. |
Example
Python
Copy Code
print(f'Is 5 of type int? {isinstance(5, int)}') class Person: pass class Address: pass aPerson = Person() print(f'Is aPerson an instance of Person? {isinstance(aPerson, Person)}') print(f'Is aPerson an instance of Address? {isinstance(aPerson, Address)}')
Output
Is 5 of type int? True Is aPerson an instance of Person? True Is aPerson an instance of Address? False