class Chain(object): def __init__(self,path=str()): self.__path=path #print self.__path,‘echo from __init__‘ def __getattr__(self,path): #print ‘called __getattr__‘ return Chain(‘%s/%s‘ %(self.__path,path)) def __str__(self): print ‘called __str__‘ return self.__path print Chain().status.other # 如果将以上代码中的注释去掉的话 echo from __init__ called __getattr__ /status echo from __init__ called __getattr__ /status/other echo from __init__ called __str__ /status/other
Chain().status返回Chain()对象,继续调用 other,如此循环
时间: 2024-11-05 18:00:00