Built-in Functions
setattr()
Set an attribute on an object using its name as text.
setattr(object, name, value)
setattr() is like writing object.name = value, but the attribute name is a string.
It is useful when an attribute name is stored in a variable.
Parameters
| Name | Description |
|---|---|
| object | The object to change. |
| name | The attribute name as a string. |
| value | The value to assign. |
Returns
None.
Try it
Run it and change it
class Box: pass box = Box() setattr(box, "colour", "blue") print(box.colour)