Search notes:

Python: setattr

It is possible to add dynamic attributes to an object just by assigning a value to a property name.
class CLS: pass

obj = CLS()

obj.attr_one = 42
obj.attr_two ='hello World'

print(obj.attr_one, obj.attr_two)
Github repository about-Python, path: /built-in/functions/setattr/dynamically-add-attributes.py
In the above script, only attribute-names can be used that are known at the time that the script was written. In order to an attribute with an arbitrary name that is stored in a variable, setattr() can be used:
class CLS: pass

obj = CLS()

setattr(obj, 'attr_one',  42          )
setattr(obj, 'attr_two', 'Hello world')

print(obj.attr_one, obj.attr_two)
Github repository about-Python, path: /built-in/functions/setattr/dynamic-names.py

See also

__setattr__
Python: Built in functions

Index