Discuss / Python / 动态调用,并且将users替换成实际的用户名

动态调用,并且将users替换成实际的用户名

Topic source

飞侠100

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

__getattr__接收self、path(属性名)作为参数,不能再接收实际用户名'Michael'作为参数,因此需要另外定义一个可以接收用户名参数的users方法

class Chain(object):
    def __init__(self, path=''):
        self._path = path

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

    def users(self, name):
        return Chain('%s/:%s' %(self._path, name))

    def __str__(self):
        return self._path

    __repr__ = __str__

  • 1

Reply