isinstance()
Checks whether a value is of a given type, or of one of several types, and also accepts subclasses, which makes it the right way to test types.
isinstance(object, classinfo)isinstance(x, int) answers one question: is x an int? It returns True or False. Give a tuple of types to accept several at once: isinstance(x, (int, float)).
Use it when a function must handle different kinds of input, for example a single name or a list of names, or when you want to reject bad data with a clear message.
Unlike type(x) == int, it also says yes to subclasses. That is usually what you want, but it brings one well-known surprise: bool is a subclass of int, so isinstance(True, int) is True. If booleans must not count as numbers, rule them out first.
Parameters
| Parameter | What it is |
|---|---|
objectrequired | The value to check. |
classinforequired | A type (int, str, your own class), a tuple of types, or, from Python 3.10, a union written with | such as int | float. |
Return value
True if the object is an instance of the type (or of a subclass of it, or of any type in the tuple), otherwise False.
Examples
1Simple checks
print(isinstance(42, int))
print(isinstance("42", int))
print(isinstance(3.5, (int, float)))
print(isinstance([1, 2], list))True False True True
2Accept one item or a list
def greet(names):
if isinstance(names, str):
names = [names]
for name in names:
print("Hi,", name)
greet("Sofia")
greet(["Kenji", "Amara"])Hi, Sofia Hi, Kenji Hi, Amara
Without the check, a single string would be looped over letter by letter.
3Add up only the numbers in mixed data
row = ["Total", 12, 4.5, None, "n/a", 3]
numbers = [v for v in row if isinstance(v, (int, float))]
print(numbers)
print(sum(numbers))[12, 4.5, 3] 19.5
4Subclasses count too
class Animal:
pass
class Dog(Animal):
pass
rex = Dog()
print(isinstance(rex, Dog))
print(isinstance(rex, Animal))
print(type(rex) == Animal)True True False
A Dog is also an Animal, so isinstance() says yes. Comparing with type() only matches the exact class.
5Union types with |
From Python 3.10 you can write the types with |, the same way type hints do:
for value in [7, 2.5, "7"]:
print(repr(value), isinstance(value, int | float))7 True 2.5 True '7' False
Common mistakes
True and False pass as int
Because bool is a kind of int, a check for numbers lets booleans through. Exclude them explicitly when that matters.
def is_number(x):
return isinstance(x, (int, float)) and not isinstance(x, bool)
print(isinstance(True, int))
print(is_number(True), is_number(5))True False True
Writing the type as a string
The second argument must be the type itself, not its name in quotes.
try:
isinstance(5, "int")
except TypeError:
print("TypeError: the second argument must be a type")
print(isinstance(5, int))TypeError: the second argument must be a type True
Questions people ask
What is the difference between type() and isinstance()?
type(x) == int matches one exact class. isinstance(x, int) also accepts subclasses. Use isinstance() for checks and type() for printing and exploring.
How do I check if a variable is a number?
isinstance(x, (int, float)). Add and not isinstance(x, bool) if True and False must not count.
How do I check if a value is a list or a tuple?
isinstance(x, (list, tuple)).