Remember
hasattr() checks whether attribute lookup succeeds without AttributeError. It does not tell you whether the attribute is callable.
hasattr() checks whether attribute lookup succeeds without AttributeError. It does not tell you whether the attribute is callable.
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.
value = 'tea'
print(hasattr(value, "upper"))
print(hasattr(value, "append"))Top-level scope
'tea'hasattr() checks whether attribute lookup succeeds without AttributeError. It does not tell you whether the attribute is callable.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
value = 'tea'
print(hasattr(value, "upper"))
print(hasattr(value, "append"))
|
v
DISPLAYED RESULT
True
False
FINAL BINDINGS
value --> object 1 (str)
object 1: 'tea'Choose 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.
value = 'tea'
print(hasattr(value, "upper"))
print(hasattr(value, "append"))Top-level scope
'tea'The example has finished. Compare the final output with the values and references above.
True False
What do you expect this program to print?
value = 'tea'
print(hasattr(value, "upper"))
print(hasattr(value, "append"))hasattr() FunctionThe hasattr() function in Python is used to check if an object has a specific attribute. It returns True if the attribute exists and False otherwise. This is especially useful when working with dynamic objects or avoiding errors when accessing unknown attributes.
hasattr(object, name)
object – The object you want to check.name – The name of the attribute (as a string).True if the attribute exists.False if it doesn’t exist or raises an AttributeError.class Person:
name = "Alice"
p = Person()
print(hasattr(p, "name")) # True
print(hasattr(p, "age")) # False
True
False
Instead of directly accessing an attribute which may or may not exist, use hasattr() to check first:
if hasattr(p, "name"):
print(p.name)
else:
print("No name attribute found")
class Car:
def __init__(self):
self.brand = "Tesla"
c = Car()
print(hasattr(c, "brand")) # True
print(hasattr(c, "__dict__")) # True
print(hasattr(c, "model")) # False
True
True
False
TypeError.False for properties that raise exceptions during lookup.hasattr() is commonly used in dynamic programming, frameworks like Django, and when working with JSON-like objects or user-defined classes.
hasattr() checks if an object has an attribute.True or False.AttributeError.Create a class Book with a title attribute. Write a program that asks the user for an attribute name and checks whether it exists using hasattr().
class Book:
def __init__(self):
self.title = "Python Guide"
b = Book()
attr = input("Enter attribute name to check: ")
if hasattr(b, attr):
print("Attribute exists")
else:
print("Attribute does not exist")
Connect what you noticed in the example with the idea below.
hasattr() checks whether attribute lookup succeeds without AttributeError. It does not tell you whether the attribute is callable.
hasattr() always calls the attribute as a method.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.