I was messing around with inherited classes and wondered if it was possible to set a custom object attribute using a method. It would work something like this:
class MyClass(object):
def __init__(self):
super.__init__()
def setCustAttr(self, name, value):
#...
g=MyClass()
g.setCustAttr("var",5)
g.var+=6
g.var="text"
exec("self."+string+"="+value)?This is what the setattr function does:
setattr(g, 'var', 5)
# g.var is now 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With