Created
October 12, 2017 01:59
-
-
Save geekKeen/d89a6e8a1d2e0ff4a10870a1528ccbbf to your computer and use it in GitHub Desktop.
代理模式 属性访问控制
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FileStorage(object): | |
def __init__(self, stream, **kwargs): | |
self.stream = stream | |
def __getattr__(self, name) | |
return getattr(self.stream, name) |
__getattr__
和 __setattr__
解析:
__getattr__
: 当实例属性和类的属性没有找到时调用, 能找就不调用, 方法计算属性或者抛出 AttributeError
异常
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
简单的代理模式只需要实现
__getattr__
方法, 使调用落到被代理对象上