To check whether an object is an instance of a class in Python, use isinstance(obj, ClassName). It returns True when the object is an instance of that class or of any subclass, and False otherwise.
You can pass a tuple of classes, or since Python 3.10 a union such as int | str, to test several types at once. Use type(obj) is ClassName only when you need an exact match that excludes subclasses.
The syntax is isinstance(object, classinfo). classinfo can be a class, a tuple of classes, or a union type.
Because isinstance honours inheritance, it is the right choice for polymorphic code, where a function should accept any subclass of an expected type.
Pass a tuple, or a union with the | operator on Python 3.10 and later, to accept any of several types.
type(obj) is Class is true only for the exact class, so it rejects subclasses. In most code that is not what you want.
The bool and int pair shows the difference. If a function takes integers but should not accept True, check isinstance(x, int) and not isinstance(x, bool). Otherwise prefer isinstance.
For containers and protocols, check against the abstract base classes in collections.abc instead of concrete types. That way custom classes that behave like a sequence or mapping also pass.
str is a Sequence too, which is why the example excludes it explicitly.
Use issubclass(Dog, Animal) to test classes instead of instances. For checking interfaces without inheritance, define a typing.Protocol marked @runtime_checkable; isinstance then checks that the required attributes exist, but not their signatures.
isinstance does not work with parameterised generics such as list[int], and raises TypeError. Check isinstance(x, list) and validate the items separately.
On Python 3.10 and later, match with class patterns performs the same check and can unpack attributes at the same time.
isinstance(x, list) rejects tuples and other sequences that your function may handle just as well; prefer an abstract base class when the behaviour matters, not the concrete type.isinstance(x, int) is True for bool, as shown earlier, which can let True slip into numeric code.__instancecheck__ on their metaclass, such as ABCs, may report True without inheriting from the class.isinstance checks for data that enters your program from outside.isinstance accepts subclasses, while type(obj) is Class requires the exact class. Use isinstance unless you specifically need to exclude subclasses.
Yes. Pass a tuple like (int, float) or a union like int | float on Python 3.10 and later.
Use callable(obj), or isinstance(obj, collections.abc.Iterable) for iterables.
Overusing it can signal that you should rely on duck typing or polymorphism. It is appropriate at input boundaries, in validation and when handling several known types.
Use isinstance(obj, Class) to check an object's type with inheritance support, a tuple or union for several types, and collections.abc classes for generic container checks. Choose type(obj) is Class only when exactness matters, and consider match statements for branching on types.