Question
How can you check whether an object is a given type in Python, including whether it inherits from that type?
For example, how can you determine whether an object o should be treated as a str?
o = "hello"
More generally:
- How do you check whether an object is exactly a specific type?
- How do you check whether an object is an instance of a type or any subclass of that type?
This question is about checking Python object types correctly. It is not about converting strings to numbers or deciding whether a string like "1" should count as an integer.
Short Answer
By the end of this page, you will understand the standard Python ways to check an object’s type: when to use isinstance(), when type() is appropriate, and how inheritance affects the result. You will also see common mistakes, practical examples, and how type checks are used in real Python code.
Concept
In Python, the canonical way to check whether an object behaves like a given type is usually isinstance(obj, SomeType).
isinstance(o, str)
This returns True if o is:
- exactly a
str, or - an instance of a subclass of
str
That matters because Python supports inheritance. A class can extend another class, and objects of the child class should often be accepted anywhere the parent type is expected.
If you want to check whether an object is exactly one specific type and not a subclass, use:
type(o) is str
or equivalently:
type(o) == str
However, type(o) is str is typically preferred over == for exact type identity.
Why this matters
Type checks appear in real programs when:
Mental Model
Think of a class as a job title and inheritance as specialization.
stris like the job title Writer.- A subclass of
stris like Technical Writer.
If a task says, "Anyone who is a Writer can do this," then both Writer and Technical Writer should qualify. That is what isinstance() does.
If the rule says, "Only someone with the exact title Writer, not any specialized version," then you need an exact match. That is what type(obj) is str does.
So:
isinstance()asks: Does this object belong to this family of types?type() isasks: Is this object exactly this one type?
Syntax and Examples
The two most common approaches are:
1. Check for a type or subclass with isinstance()
o = "hello"
print(isinstance(o, str))
Output:
True
This is the standard approach in Python.
2. Check for the exact type with type()
o = "hello"
print(type(o) is str)
Output:
True
This only matches if o is exactly a str.
Example with inheritance
class MyString(str):
pass
value = MyString()
((value, ))
((value) )
((value) MyString)
Step by Step Execution
Consider this example:
class Animal:
pass
class Dog(Animal):
pass
pet = Dog()
print(isinstance(pet, Dog))
print(isinstance(pet, Animal))
print(type(pet) is Dog)
print(type(pet) is Animal)
Here is what happens step by step:
Animalis defined.Dogis defined as a subclass ofAnimal.pet = Dog()creates aDogobject.isinstance(pet, Dog)checks whetherpetis aDogor subclass ofDog.- Result:
True
- Result:
isinstance(pet, Animal)checks whether is an or subclass of .
Real World Use Cases
Input validation
A function may require text input:
def save_username(name):
if not isinstance(name, str):
raise TypeError("name must be a string")
# save name
Accepting several numeric types
def square(x):
if not isinstance(x, (int, float)):
raise TypeError("x must be numeric")
return x * x
Handling data from APIs
When parsing JSON-like data, you may need to confirm the shape of a value:
def process_payload(data):
if not isinstance(data, dict):
raise TypeError("data must be a dictionary")
Working with custom class hierarchies
Real Codebase Usage
In real projects, developers often use type checks in controlled places rather than everywhere.
Guard clauses
A common pattern is to reject invalid input early:
def normalize_text(value):
if not isinstance(value, str):
raise TypeError("value must be a string")
return value.strip().lower()
This keeps the rest of the function simpler.
Validation at module boundaries
Type checks are common where data enters the system:
- API request handlers
- file readers
- command-line argument processing
- deserialization code
Once data is validated, the rest of the code can assume correct types.
Accepting abstract parent types
Instead of checking for one exact class, code often checks for a parent type so subclasses also work:
def handle_error(err):
if isinstance(err, Exception):
print(f"Error: {err}")
This is flexible and works with built-in and custom exception classes.
Early return patterns
Common Mistakes
1. Using type() when inheritance should be allowed
Broken idea:
class MyString(str):
pass
value = MyString("hello")
print(type(value) is str) # False
If your code should accept subclasses, use:
print(isinstance(value, str)) # True
2. Using == with type() unnecessarily
type(o) == str
This works for exact type comparison, but is better expresses that you are comparing type identity:
type(o) is str
3. Confusing type checking with conversion
Comparisons
| Approach | What it checks | Inheritance-aware? | Typical use |
|---|---|---|---|
isinstance(obj, T) | Whether obj is an instance of T or a subclass of T | Yes | Most type checks |
type(obj) is T | Whether obj is exactly type T | No | Exact type match only |
type(obj) == T | Also checks exact type | No | Less preferred than is for types |
isinstance() vs
Cheat Sheet
Quick rules
- Use
isinstance(obj, T)for most type checks. - Use
type(obj) is Tonly for exact matches. isinstance()supports inheritance.isinstance()can check multiple types with a tuple.
Common syntax
isinstance(obj, str)
isinstance(obj, (int, float, complex))
type(obj) is str
Examples
isinstance("hi", str) # True
type("hi") is str # True
class MyStr(str):
pass
s = MyStr("hello")
isinstance(s, str) # True
type(s)
FAQ
Should I use isinstance() or type() in Python?
Usually use isinstance(). It correctly handles inheritance and is the standard choice for most checks.
How do I check if a value is a string in Python?
Use:
isinstance(value, str)
How do I check for an exact type in Python?
Use:
type(value) is str
This does not count subclasses.
Does isinstance() work with subclasses?
Yes. That is one of the main reasons it is preferred.
Can isinstance() check multiple types?
Yes:
isinstance(value, (int, float))
Why is isinstance(True, int) true?
In Python, bool is a subclass of . If you need exact , use .
Mini Project
Description
Build a small validation utility that checks whether incoming values match expected Python types. This mirrors real code where data enters from user input, APIs, or configuration files and must be validated before use.
Goal
Create a function that accepts a value and verifies whether it is a string, an exact integer, or one of several allowed types.
Requirements
- Write a function that checks whether a value is a string using
isinstance(). - Write a function that checks whether a value is exactly an
int, notbool. - Write a function that accepts values that are either
intorfloat. - Print clear results for several sample values.
- Use both
isinstance()andtype() iswhere each one makes sense.
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Call a Function by Name in a Python Module
Learn how to call a function by name in a Python module using strings, getattr, and safe patterns for dynamic function dispatch.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.