How do you set/get the values of attributes of t given by x?
t
x
class Test: def __init__(self): self.attr1 = 1 self.attr2 = 2 t = Test() x = "attr1"
There are built-in functions called getattr and setattr
getattr
setattr
getattr(object, attrname) setattr(object, attrname, value)
In this case
x = getattr(t, 'attr1') setattr(t, 'attr1', 21)
1.4m articles
1.4m replys
5 comments
57.0k users