Remember
isinstance() checks whether an object belongs to a class or its subclasses.
isinstance() checks whether an object belongs to a class or its subclasses.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
number = 3
print(isinstance(number, int))
print(isinstance(number, str))Top-level scope
3isinstance() checks whether an object belongs to a class or its subclasses.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
number = 3
print(isinstance(number, int))
print(isinstance(number, str))
|
v
DISPLAYED RESULT
True
False
FINAL BINDINGS
number --> object 1 (int)
object 1: 3Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
number = 3
print(isinstance(number, int))
print(isinstance(number, str))Top-level scope
3The example has finished. Compare the final output with the values and references above.
True False
What do you expect this program to print?
number = 3
print(isinstance(number, int))
print(isinstance(number, str))isinstance() FunctionThe isinstance() function in Python checks whether a variable is an instance of a specific class or a tuple of classes. It's a safe and recommended way to validate data types.
isinstance(object, classinfo)
object – The value or variable to test.classinfo – A class, type, or a tuple of classes and types.True if object is an instance (or subclass instance) of classinfo.False otherwise.x = 5
print(isinstance(x, int))
True
name = "Alice"
print(isinstance(name, list))
False
value = 3.14
print(isinstance(value, (int, float)))
True
Here, value is checked against both int and float. The function returns True if it matches any one.
def square(x):
if isinstance(x, (int, float)):
return x * x
else:
return "Invalid input"
print(square(4)) # 16
print(square("four")) # Invalid input
16
Invalid input
classinfo like "int" (should be int without quotes)type() with isinstance(): isinstance() supports inheritance, type() does notisinstance() is often used in questions involving type safety or when a function needs to behave differently based on data types. It’s also commonly used in object-oriented questions.
isinstance(obj, type) returns True if obj is of the given type or its subclass.type() when inheritance is involved.Write a function that accepts a value and returns:
"Integer" if it’s an integer"Float" if it’s a float"Other" otherwisedef identify_type(x):
if isinstance(x, int):
return "Integer"
elif isinstance(x, float):
return "Float"
else:
return "Other"
print(identify_type(42))
print(identify_type(3.14))
print(identify_type("hello"))
Integer
Float
Other
Connect what you noticed in the example with the idea below.
isinstance() checks whether an object belongs to a class or its subclasses.
isinstance() tests whether two objects have equal values.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.