isinstance()
The subclass-aware type check — the tool you almost always want instead of `type(x) is C`.
Common call
isinstance(x, int)
Returns
True or False
Replaces
`type(x) is C` when subclasses should count
Watch out
bool is a subclass of int — isinstance(True, int) is True
isinstance(objectobject — Any value. isinstance walks its type's MRO (method resolution order) looking for a match with classinfo.type: Any · required, classinfoclassinfo — A class, or a tuple of classes. Tuple form is True if any element matches — equivalent to `isinstance(x, A) or isinstance(x, B)` but faster and cleaner.type: type | tuple[type] · required)
→ bool
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| object | Any | yes | Any value. isinstance walks its type's MRO (method resolution order) looking for a match with classinfo. |
| classinfo | type | tuple[type] | yes | A class, or a tuple of classes. Tuple form is True if any element matches — equivalent to `isinstance(x, A) or isinstance(x, B)` but faster and cleaner. |
Return value
bool — True if object is an instance of classinfo — or of ANY subclass of classinfo. classinfo can be a single class or a tuple of classes; True if any match. Never raises for a normal class; raises TypeError if classinfo is not a class or tuple of classes.
Common patterns
Runtime type check
Guard against wrong types with a friendly message.
if not isinstance(config, dict): raise TypeError("config must be a dict")
Multi-type check with a tuple
Accept several types in one call — cleaner than chained `or`.
if isinstance(x, (int, float)): ... # any numeric
Duck typing hint — prefer behavior over class
Sometimes the right check is "does it have this method?", not "is it this class?".
if hasattr(x, "__iter__"): for item in x: ...
Structural check with ABC
abc classes like Iterable, Mapping, Sequence work with isinstance.
from collections.abc import Mapping if isinstance(x, Mapping): for k, v in x.items(): ...
Examples
1. Basic string
isinstance("hi", str)
Returns
True2. Basic int
isinstance(42, int)
Returns
True3. Wrong class
isinstance("hi", int)
Returns
False4. Tuple of classes
isinstance(3.14, (int, float))
Returns
True5. Bool is int
isinstance(True, int)
Returns
True # bool subclasses int6. Int is NOT bool
isinstance(42, bool)
Returns
False # not the other way7. None is NoneType
isinstance(None, type(None))
Returns
True8. Non-class raises
isinstance(x, "int")
Returns
TypeError: isinstance() arg 2 must be a typePitfalls
1. bool is a subclass of int
The single most surprising isinstance result. Historically booleans came from integers, so True and False satisfy isinstance(x, int). If you specifically want "an int but not a bool", filter out bools explicitly.
Accepts True
isinstance(True, int)
True
Filter bool out
isinstance(x, int) and not isinstance(x, bool)
True only for real int
2. The classinfo must be a type, not a name
A common typo — passing the CLASS NAME as a string instead of the class itself.
String argument
isinstance(x, "int")
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
Class object
isinstance(x, int)
True or False
3. isinstance vs type() — different semantics
type() is exact; isinstance() is subclass-aware. Use type() only when subclasses should NOT count — dispatch tables keyed by exact class are the rare valid use.
Exact miss
class MyDict(dict): pass x = MyDict() type(x) is dict
False # exact class only
Subclass ok
isinstance(x, dict)
True
4. Tuple form: parentheses matter
For a multi-type check, wrap the classes in a tuple. Without the tuple, Python sees only the first argument.
Missing tuple
isinstance(x, int, float)
TypeError: isinstance expected 2 arguments, got 3
Tuple wrap
isinstance(x, (int, float))
True or False
When to use
Use it
- Runtime type validation with helpful error messages
- Multi-type acceptance via a tuple (numeric = int OR float)
- Duck-typing structural checks against collections.abc classes
- When subclass instances should count as the base class
Reach for something else
- Exact-class dispatch tables → type() is correct
- Detecting None → `x is None` (fastest and clearest)
- Deep behavior checks → hasattr / try-except may be more idiomatic
- Type checking beyond runtime → static type checkers with type hints
Notes
Complexity
O(mro depth) — walks the class hierarchy once
Return
bool — True or False
CPython impl
Python/bltinmodule.c :: builtin_isinstance — calls PyObject_IsInstance
Memory
No allocation
Thread-safe
Yes
FAQ
isinstance is subclass-aware — True if the object is an instance of the class OR any subclass. type() checks EXACT class. Use isinstance for most runtime checks; use type() only for exact-class dispatch.
History
1.5
isinstance() introduced along with the type hierarchy overhaul.
2.2
Support for tuple of classes as classinfo.
3.10
`isinstance(x, int | str)` supported via PEP 604 union type expressions.