Built-in Functions
isinstance()
Check whether a value is an instance of a type.
isinstance(object, classinfo)
isinstance() returns True if the object is an instance of classinfo or a subclass thereof.
Supports checking against a tuple or union (int | str in Python 3.10+) of multiple types.
Parameters
| Name | Description |
|---|---|
| object | The value to test. |
| classinfo | A type, class, or tuple of types and classes. |
Returns
True if the object matches the given type or class; otherwise False.
Try it
Run it and change it
value = 3 print(isinstance(value, int))
Worth knowing
- isinstance vs type(): Always prefer
isinstance(obj, Class)overtype(obj) == Classbecauseisinstancecorrectly supports class inheritance and subclasses.