把一个类中的方法变成属性,只需要加上@property就可以了
例子
class opt(object):
def __init__(self, root):
super(opt, self).__setattr__('root_path', root)
@property
def filename(self):
return self.root_path
a = opt('chenju')
a.filename
>>>'chenju'