Discuss / Python / 作业

作业

Topic source

Super-String

#1 Created at ... [Delete] [Delete and Lock User]
class Chain(object):
    def __init__(self, path=''):
        self._path = path



    def __getattr__(self, path):
        return Chain('%s/%s' % (self._path, path))



    def __call__(self, user):
        return Chain('%s/%s' % (self._path.replace(':user', user), ''))



    def __str__(self):
        return self._path



    __repr__ = __str__

```

使用该类,可以这样调用 GitHub API:

```python

Chain().users(':user').repos(user='michael')

```

这将返回一个 `Chain` 类实例,其路径为 "/users/michael/repos"。


  • 1

Reply