Junior — Middle
How can you get or modify the value of an object's attribute in Python?
sobes.tech AI
Answer from AI
In Python, the value of an object's attribute can be accessed and modified in several ways:
-
Using dot notation:
obj.attribute # get obj.attribute = value # set -
Using the
getattrandsetattrfunctions:value = getattr(obj, 'attribute') setattr(obj, 'attribute', new_value) -
Directly working with the object's
__dict__(if it exists):obj.__dict__['attribute'] = new_value
These methods allow flexible management of object attributes at runtime.