Discuss / Python / 不使用__call__的做法

不使用__call__的做法

Topic source

岁益寒

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

# -*- coding: utf-8 -*-

class Chain(object):

    def __init__(self, path=''):

        self._path = path

    def __getattr__(self, path):

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

    def __str__(self):

        return self._path

    def users(self, path):

        return Chain('/users/:%s' % (path))

    __repr__ = __str__

print(Chain().status.user.timeline.list)

print(Chain().users('michael').repos)

岁益寒

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

# -*- coding: utf-8 -*-

class Chain(object):

# 借鉴评论区

    def __init__(self, path=''):

        self._path = path

    def __getattr__(self, path):

        if path == 'users':

            return lambda name: Chain('%s/%s/:%s' % (self._path, path, name))

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

    def __str__(self):

        return self._path

    __repr__ = __str__

print(Chain().status.user.timeline.list)

print(Chain().users('michael').repos)


  • 1

Reply