Remember
setattr() assigns an attribute by its name, when the object allows that assignment.
setattr() assigns an attribute by its name, when the object allows that assignment.
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()
setattr(box, "value", 3)
print(box.value)Top-level scope
class BoxObject 2 · BoxBox({'value': 3})setattr() assigns an attribute by its name, when the object allows that assignment.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
class Box:
pass
box = Box()
setattr(box, "value", 3)
print(box.value)
|
v
DISPLAYED RESULT
3
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()
setattr(box, "value", 3)
print(box.value)Top-level scope
class BoxObject 2 · BoxBox({'value': 3})The example has finished. Compare the final output with the values and references above.
3
What do you expect this program to print?
class Box:
pass
box = Box()
setattr(box, "value", 3)
print(box.value)setattr() FunctionThe setattr() function in Python lets you set or update the value of an attribute of an object dynamically. It’s commonly used when you don't know the attribute name in advance and need to assign it programmatically.
setattr(object, name, value)
object – The object whose attribute is to be set.name – A string, the name of the attribute to set.value – The value to assign to the attribute.class Person:
pass
p = Person()
setattr(p, 'name', 'Alice')
print(p.name)
Alice
Explanation: We dynamically added a name attribute to the Person object using setattr().
class Car:
def __init__(self):
self.color = "Red"
c = Car()
setattr(c, 'color', 'Blue')
print(c.color)
Blue
Explanation: The existing color attribute was updated from "Red" to "Blue".
class Item:
pass
item = Item()
attrs = {'name': 'Book', 'price': 250, 'stock': 100}
for key, value in attrs.items():
setattr(item, key, value)
print(item.name, item.price, item.stock)
Book 250 100
Explanation: Multiple attributes were added dynamically using setattr() inside a loop.
TypeError.int or str) will raise an AttributeError.Use setattr() to create flexible classes in object-oriented design. It’s useful in metaprogramming, decorators, and frameworks like Django or Flask.
setattr() dynamically sets or updates an attribute on an object.Write a Python class Student and dynamically add name, grade, and subject using setattr(). Then print all three attributes.
class Student:
pass
s = Student()
setattr(s, 'name', 'Ravi')
setattr(s, 'grade', 'A')
setattr(s, 'subject', 'Math')
print(s.name)
print(s.grade)
print(s.subject)
Connect what you noticed in the example with the idea below.
setattr() assigns an attribute by its name, when the object allows that assignment.
setattr() assigns a key in every dictionary automatically.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.