Discuss / Python / 把:user替换为实际用户名

把:user替换为实际用户名

Topic source

zicozicozi

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

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

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

def __str__(self):
    return self._path
__repr__ = __str__

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

GET /users/michael/repos

zicozicozi

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

在传入参数users时调用getattr返回Chain().getattr,这时Chain().users('Michael'),就相当于Chain().getattr('Micheal'),python里函数名和变量是一样的,都是引用,而且在对象里都是以字符串的形式储存在列表里,函数名()==变量名()


  • 1

Reply