isinstance()
IntrospectionReturns True if the object is an instance of the class or a subclass thereof.
Signature
isinstance(object, classinfo)
Returns
boolExample
print(isinstance(42, int)) # True
print(isinstance('hi', str)) # True
print(isinstance([], (list, tuple))) # TrueAbout isinstance()
isinstance is a Python introspection function with the signature isinstance(object, classinfo). Returns True if the object is an instance of the class or a subclass thereof. It returns a value of type bool.
Python provides a rich set of built-in functions and standard library modules that cover common programming tasks. Understanding these functions helps you write more idiomatic, efficient Python code. The isinstancefunction is commonly used in data processing, web development, scripting, and automation tasks.
When working with isinstance(), consider edge cases like empty inputs, None values, and type mismatches. Python's duck typing means many built-in functions work with any object that implements the required protocol (e.g., __len__ for len(), __iter__ for iteration). This flexibility is a key strength of Python's design philosophy.