代码:
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 print Chain().login.user.password
输出:
/login/user/password
备注与说明:
Chain().login.user.password
等价于以下任意一行语句
Chain("/login").user.password Chain("/login/user").password Chain("/login/user/password")