Remember
getattr() retrieves an attribute by its name. A default handles a missing attribute when supplied.
getattr() retrieves an attribute by its name. A default handles a missing attribute when supplied.
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.
class Box:
pass
box = Box()
box.value = 3
print(getattr(box, "value"))
print(getattr(box, "missing", "not found"))Top-level scope
class BoxObject 2 · BoxBox({'value': 3})getattr() retrieves an attribute by its name. A default handles a missing attribute when supplied.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
class Box:
pass
box = Box()
box.value = 3
print(getattr(box, "value"))
print(getattr(box, "missing", "not found"))
|
v
DISPLAYED RESULT
3
not found
FINAL BINDINGS
Box --> object 1 (type)
box --> object 2 (Box)
object 1: class Box
object 2: Box({'value': 3})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.
class Box:
pass
box = Box()
box.value = 3
print(getattr(box, "value"))
print(getattr(box, "missing", "not found"))Top-level scope
class BoxObject 2 · BoxBox({'value': 3})The example has finished. Compare the final output with the values and references above.
3 not found
What do you expect this program to print?
class Box:
pass
box = Box()
box.value = 3
print(getattr(box, "value"))
print(getattr(box, "missing", "not found"))getattr() FunctionThe getattr() function in Python is used to get the value of an attribute of an object dynamically. It's especially useful when the attribute name is stored as a string or determined at runtime.
getattr(object, name[, default])
object – The object whose attribute you want to access.name – A string representing the attribute name.default (optional) – A value to return if the attribute does not exist.default is provided, it returns default.default is given, it raises an AttributeError.class Person:
name = "Alice"
age = 30
p = Person()
print(getattr(p, 'name'))
Alice
Why? Because 'name' is an attribute of object p.
print(getattr(p, 'gender', 'Not Specified'))
Not Specified
This avoids an error and returns a default string if the attribute does not exist.
attr_name = input("Enter attribute (name or age): ")
print(getattr(p, attr_name, "Attribute not found"))
Why is this useful?
name will raise a TypeErrordefault can lead to AttributeError if attribute doesn’t existgetattr() is often used in interview questions that involve object introspection or building generic class utilities.
getattr() fetches an attribute from an object by nameCreate a class Car with attributes brand and year. Write a program that asks the user to enter an attribute name and prints the value using getattr(). If the attribute doesn’t exist, print "Unknown attribute".
class Car:
brand = "Toyota"
year = 2022
c = Car()
attr = input("Enter attribute (brand/year): ")
print(getattr(c, attr, "Unknown attribute"))
Connect what you noticed in the example with the idea below.
getattr() retrieves an attribute by its name. A default handles a missing attribute when supplied.
getattr() creates the missing attribute when given a default.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.