Python isinstance() function
Check whether a value is of a given type (or one of several types).
Syntax
isinstance(obj, classinfo)
Returns True or False. Give a tuple of types to accept any of them. It also accepts subclasses, which is why it is preferred over comparing type(x) == ….
Parameters
| obj | the value to test |
| classinfo | a type, or a tuple of types |
Return value
bool
Example
Code
print(isinstance(5, int)) print(isinstance(5.0, int)) print(isinstance(5.0, (int, float))) print(isinstance("5", str)) print(isinstance(True, int)) # True: bool is a kind of int
Output
True False True True True
Related
Learn it step by step in the free lesson Classes & Objects, with exercises that check your answers.