Created
August 16, 2017 03:54
-
-
Save geekKeen/e2ca597d138b862514b56ef89ee6169d 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 A(object): | |
def __setattr__(self, attr, value): | |
self.__dict__(self, attr, value) # setattr(self, attr, value) | |
def __getattr__(self, attr): | |
return self.__dict__(self, attr) # getattr(self, attr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
warning:
setattr(x, 'foo', 123) 等价于 x.foo = 123 会调用 setattr, 如果自定义实现了 setattr 将会造成死循环
内部可以使用 object.setattr