In
Now if you want to set an attribute called name on
With a little bit of code you can automatically get data from a source and set dynamic attributes.
#python #setattr
Python
you can use setattr
to set an attribute on a class. Let's say we have a very simple class like below:class User:
def register(self, key, val):
setattr(self, key, val)
Now if you want to set an attribute called name on
User
class:>>> user = User()
>>> user.register('name', 'alireza')
>>> user.name
'alireza'
With a little bit of code you can automatically get data from a source and set dynamic attributes.
#python #setattr