Skip to main content

How to Check a Value's Type in Python with isinstance

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

isinstance(value, str) answers one question: is this value a string? It returns True or False, it can test for several types at once, and it has one surprise worth knowing before you rely on it: True and False count as integers.

Syntax​

Syntax
isinstance(<value>, <type>)
isinstance(<value>, (<type>, <type>))
isinstance(<value>, <type> | <type>)
PartWhat it does
<value>The value to test: a variable, a literal or any expression.
<type>A type such as str, int, float, list or dict, written as a name, without quotes.
(<type>, <type>)A tuple of types. The result is True when the value is an instance of any of them.
<type> | <type>A union of types, which works the same way as the tuple.

The Python documentation for isinstance(): "Return True if the object argument is an instance of the classinfo argument, or of a (direct, indirect, or virtual) subclass thereof. If object is not an object of the given type, the function always returns False."

The Example​

The example tests eight values: the string "Ada", the number 42, the number 3.5 against int, a list, a dictionary, None against dict, 3.5 against the tuple (int, float), and True against int. Each line prints True or False.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Code​

Create a file named main.py:

main.py, which prints isinstance of the string Ada and str, 42 and int, 3.5 and int, a list and list, a dictionary and dict, None and dict, 3.5 and the tuple int, float, and True and int.

main.py
print(isinstance("Ada", str))
print(isinstance(42, int))
print(isinstance(3.5, int))
print(isinstance(["a", "b"], list))
print(isinstance({"name": "Ada"}, dict))
print(isinstance(None, dict))
print(isinstance(3.5, (int, float)))
print(isinstance(True, int))

Step 2: Run It​

Terminal
uv run python main.py

uv run python main.py runs the file with the Python that uv manages.

Output
True
True
False
True
True
False
True
True

A zsh terminal where uv run python main.py prints True, True, False, True, True, False, True and True.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • The first two lines are True. "Ada" is a str, and 42 is an int.
  • isinstance(3.5, int) is False. 3.5 is a float, and a float isn't an int, though both are numbers.
  • The list and the dictionary passed, and None didn't. A missing value fails a type check, so a check like isinstance(value, dict) also catches None before your code reads the value.
  • The tuple (int, float) accepted 3.5, because float is one of the types in it.
  • isinstance(True, int) printed True. The documentation for bool says: "The bool class is a subclass of int". A check for a whole number lets True and False through.

Common Mistakes​

MistakeWhat happensDo this instead
Writing the type's name in quotes: isinstance(x, "str")TypeError: isinstance() arg 2 must be a type, a tuple of types, or a unionWrite the type itself: isinstance(x, str)
Testing for a list of strings with isinstance(x, list[str])TypeError: isinstance() argument 2 cannot be a parameterized genericTest isinstance(x, list), then check the items
Testing a number with isinstance(x, int) when True and False must not passisinstance(True, int) is Trueisinstance(x, int) and not isinstance(x, bool)
Expecting 10.0 to pass a check for intisinstance(10.0, int) is FalseUse (int, float) when both are fine

Troubleshooting​

SymptomLikely causeFix
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a unionThe second argument is text or another value, not a typePass a type, a tuple of types, or a union such as int | float
TypeError: isinstance() argument 2 cannot be a parameterized genericThe second argument is a type hint such as list[str]Pass the plain type, such as list
True got through a check for numbersbool is a subclass of intRule out bool first

FAQ​

What's the difference between isinstance() and type()?​

type(value) gives the value's exact type, while isinstance() also accepts subclasses. type(True) == int is False, but isinstance(True, int) is True.

Can isinstance() check for several types at once?​

Yes. Pass a tuple, isinstance(x, (int, float)), or a union, isinstance(x, int | float). Both returned True for 3.

How do I check for None?​

value is None. PEP 8, Python's style guide, says: "Comparisons to singletons like None should always be done with is or is not, never the equality operators."

Which types does data from json.loads have?​

json.loads returns str, int, float, bool, None, list and dict, so isinstance() can check each value it gives you before your code uses it.

Conclusion​

isinstance(value, type) tells you whether a value has the type your code expects. Pass a tuple or a union for several types, write the type without quotes, and rule out bool when only real numbers should pass.

Additional Resources​