Discuss / Python / 关于Chain().users('michael').repos代码的实现,不用__call__,这样有问题吗?

关于Chain().users('michael').repos代码的实现,不用__call__,这样有问题吗?

Topic source
class Chain(object):
    def __init__(self, path = ''):
        self._path = path
    def users(self, name):
        return Chain('/users/%s' % name)
    def __getattr__(self, path):
        return Chain('%s/%s' % (self._path, path))
    def __str__(self):
        return self._path
    __repr__ = __str__

#调用
print(Chain().users('michael').repos)

三睡青年

#2 Created at ... [Delete] [Delete and Lock User]

这样的话只能针对users()这一个方法,对于其他的比如还需要post(‘xxx’)就需要再写一个post函数,会比较麻烦,直接用__call__就两个都可以匹配


  • 1

Reply