-
Notifications
You must be signed in to change notification settings - Fork 1
Home
xan edited this page May 17, 2015
·
2 revisions
__getattr__ vs getattr():
- 前者attribute不存在时被调用,__getattribute__总被调用
- http://stackoverflow.com/questions/1944625/what-is-the-relationship-between-getattr-and-getattr
- http://stackoverflow.com/questions/4295678/understanding-the-difference-between-getattr-and-getattribute
使用__getattribute__时需注意避免错误:maximum recursion depth exceeded in cmp。
def __getattribute__(self, item):
if item == 'id':
return self.hex_id(object.__getattribute__(self, item))
elif item == 'fmt':
return self.Format.to_str(object.__getattribute__(self, item))
elif item == 'flag':
return self.Flag.to_str(object.__getattribute__(self, item))
else:
return object.__getattribute__(self, item)