Content · Programming · Python

Python isinstance() Function: Usage and Examples

Check whether a value is an instance of one or more types.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Check whether a value is an instance of one or more types.

How It Works


isinstance(value, classinfo) understands inheritance and accepts a type, tuple of types, or certain union type expressions. It is generally preferable to exact type() comparison when subclasses are valid.

For interface-based behavior, duck typing or typing.Protocol may be more appropriate.

Step-by-Step Example


sample_values = [42, "ready", 3.5]
for sample_value in sample_values:
    print(isinstance(sample_value, (int, float)))

Understanding the Example


  • The expression '[42, "ready", 3.5]' supplies the value stored as 'sample_values'. This value is then used or checked by the later statements.
  • The for loop processes one item per iteration rather than requiring a separate statement for each value.

A successful run of this example prints the following output:

True
False
True

Where This Fits in a Real Program


A class is useful when data and the operations that preserve its rules belong together. Distinguish class-level attributes from per-instance state, and make invalid operations fail clearly. Inheritance is not always needed: simpler composition or a standalone function may be easier to maintain.

Practice: Try It Yourself


Test several numbers and strings using a tuple of allowed types.

For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.

Further Reading


Read the official Python documentation for complete parameters, return types and exceptions.

Common Beginner Mistakes


  • Using type(value) is ExpectedClass and accidentally excluding subclass instances.
  • Forgetting that bool is a subclass of int and passes isinstance(True, int).

Key Takeaways


  • Check whether a value is an instance of one or more types.
  • isinstance(value, classinfo) understands inheritance and accepts a type, tuple of types, or certain union type expressions.
  • For interface-based behavior, duck typing or typing.Protocol may be more appropriate.
Share this resourceSend this page to someone who may find it useful.