Junior — Middle
Can you explain what the isinstance function does in Python?
sobes.tech AI
Answer from AI
The isinstance function in Python is used to check whether an object is an instance of a specified class or a tuple of classes. It returns True if the object belongs to this class or its subclass, and False otherwise.
Example usage:
x = 10
if isinstance(x, int):
print("x is an integer")
# Check for multiple types
if isinstance(x, (int, float)):
print("x is a number")
This is useful for safely checking the type before performing operations that require a specific data type.