Fundamentals 10 min read

Understanding Python’s isinstance() and type() Functions: Syntax, Usage, and Differences

This article explains the purpose, syntax, parameters, return values, and practical examples of Python’s isinstance() and type() functions, compares their behavior regarding inheritance and polymorphism, and provides advanced usage scenarios for accurate type checking in code.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Understanding Python’s isinstance() and type() Functions: Syntax, Usage, and Differences

isinstance() is a built‑in Python function used to check whether an object is an instance of a specified class or a subclass thereof, supporting polymorphism across inheritance hierarchies.

Syntax: isinstance(object, classinfo)

Parameters: object – the object to test; classinfo – a class or a tuple of classes.

Return value: True if the object is an instance of the given class or its subclass, otherwise False .

class Parent:
    pass
class Child(Parent):
    pass
class GrandChild(Child):
    pass
parent = Parent()
child = Child()
grand_child = GrandChild()
print(isinstance(parent, Parent))       # True
print(isinstance(child, Parent))        # True
print(isinstance(grand_child, Parent))  # True
print(isinstance(grand_child, Child))   # True
print(isinstance(grand_child, GrandChild))  # True
print(isinstance(grand_child, (Parent, Child)))  # True

type() returns the exact type (class) of an object, without considering inheritance.

Syntax: type(object)

Parameter: object – the object whose type is requested.

Return value: the object's class.

class Parent:
    pass
class Child(Parent):
    pass
class GrandChild(Child):
    pass
parent = Parent()
child = Child()
grand_child = GrandChild()
print(type(parent))           #
print(type(child))            #
print(type(grand_child))      #

Comparison Summary

Purpose: isinstance() checks for class or subclass membership; type() retrieves the object's exact class.

Polymorphism: isinstance() supports polymorphic checks across inheritance chains; type() does not.

Return values: isinstance() returns a Boolean; type() returns the class object.

Typical use cases: Use isinstance() when you need to verify that an object conforms to a certain interface or base class; use type() when you need the precise type, such as distinguishing int from float .

Advanced Examples

Basic type checking

# Define basic data types
integer_value = 42
float_value = 3.14
string_value = "Hello, world!"
list_value = [1, 2, 3]
tuple_value = (1, 2, 3)
dict_value = {"key": "value"}
# Using isinstance
print(isinstance(integer_value, int))   # True
print(isinstance(float_value, float))   # True
print(isinstance(string_value, str))    # True
print(isinstance(list_value, list))     # True
print(isinstance(tuple_value, tuple))   # True
print(isinstance(dict_value, dict))      # True
# Using type
print(type(integer_value) == int)   # True
print(type(float_value) == float)   # True
print(type(string_value) == str)    # True
print(type(list_value) == list)     # True
print(type(tuple_value) == tuple)   # True
print(type(dict_value) == dict)     # True

Inheritance relationship checking

class Base:
    pass
class Derived(Base):
    pass
base_instance = Base()
derived_instance = Derived()
print(isinstance(base_instance, Base))        # True
print(isinstance(derived_instance, Base))    # True
print(isinstance(derived_instance, Derived)) # True
print(type(base_instance) == Base)           # True
print(type(derived_instance) == Base)       # False
print(type(derived_instance) == Derived)      # True

Multiple inheritance

class A:
    pass
class B:
    pass
class C(A, B):
    pass
c_instance = C()
print(isinstance(c_instance, A))   # True
print(isinstance(c_instance, B))   # True
print(isinstance(c_instance, C))   # True
print(type(c_instance) == A)      # False
print(type(c_instance) == B)      # False
print(type(c_instance) == C)      # True

Checking with class name

class MyClass:
    pass
instance = MyClass()
print(isinstance(instance, MyClass))          # True
print(type(instance) == MyClass)              # True
print(instance.__class__.__name__ == "MyClass")  # True

Checking against a tuple of types

# Create different data types
integer_value = 42
float_value = 3.14
string_value = "Hello, world!"
list_value = [1, 2, 3]
print(isinstance(integer_value, (int, float)))   # True
print(isinstance(float_value, (int, float)))   # True
print(isinstance(string_value, (int, float)))  # False
print(isinstance(list_value, (int, float)))     # False
print(type(integer_value) in (int, float))       # True
print(type(float_value) in (int, float))       # True
print(type(string_value) in (int, float))      # False
print(type(list_value) in (int, float))        # False

Checking module type (illustrative)

import math
# Incorrect: isinstance(math, module) – 'module' is not a built‑in type
print(math.__name__ == "math")  # True

Conclusion

Through these examples we see that isinstance() is better suited for checking whether an object belongs to a particular class hierarchy, supporting polymorphism, while type() provides the object's exact class without considering inheritance. In most practical code, isinstance() is recommended for flexible type checks, and type() is used when the precise type is required.

programming fundamentalstype checkingtypeinheritanceisinstance
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.