Discuss / Python / 链式调用仿GitHub API

链式调用仿GitHub API

Topic source

离群之刺

#1 Created at ... [Delete] [Delete and Lock User]
class Chain:    def __init__(self, path=''):        self._path = path    def __getattr__(self, path):        return Chain(f'{self._path}/{path}')    def __str__(self):        return self._path

    __repr__ = __str__    def __call__(self, *args, **kwargs):        if args is not None:            i = self._path.rfind('/')            self._path = self._path[:i+1] + args[0]        return selfc = Chain()print(c.user('michael').repos)

  • 1

Reply