使用
property
class Test:
def __init__(self):
with open('102.yaml', 'rb') as f:
attrib_list = yaml.load(f)
_list = []
for name in attrib_list['attributes']:
_list.append(self.__setattr__('_' + name, None))
setattr(self.__class__, name,
property( Test.getprop(self,name), Test.setprop(self,name)))
@staticmethod
def getprop(self,name):
def xget(self):
print("Get {}".format(name))
return name
return xget
@staticmethod
def setprop(self,name):
def xset(self,value):
print("Set {} to {}".format(name,value))
return xset
>>> zz = Test()
>>> zz.a = "hallo"
Set a to hallo
>>> print(zz.a)
Get a
a