问题描述
how do you set/get the values of attributes of t given by x?
class test: def __init__(self): self.attr1 = 1 self.attr2 = 2 t = test() x = "attr1"
百家乐凯发k8的解决方案
there are built-in functions called getattr and setattr
getattr(object, attrname) setattr(object, attrname, value)
in this case
x = getattr(t, 'attr1') setattr(t, 'attr1', 21)