正在寻找建造弦乐工厂。我可以使用它,但是想知道是否可以使其更智能?这个想法是要使对象的字段/属性可以一个接一个地添加
#!/usr/bin/python3
# encoding: utf-8
from utils.file_utils import read_yaml
from functools import wraps
def point(name):
def wrapper(f):
@wraps(f)
def wrapped(self, *f_args, **f_kwargs):
return f(self, *f_args, **f_kwargs).add_point(name)
return wrapped
return wrapper
class EndpointSet:
def __init__(self, path: str=None):
self.dir = ''
def __repr__(self):
return self.dir
def add_point(self, name: str):
self.dir = '_'.join([self.dir, name])
@property
def home(self):
self.add_point('home')
return self
@property
def descending(self):
self.add_point('descending')
return self
if __name__ == '__main__':
print(EndpointSet().home.descending)
pass
>>> home_descending
是否可以用装饰器做类似的事情?
@property
@point('home')
def home(self):
# self.add_point('home')
return self