python __get__ __getattr__ __getattribute__
2014-07-16 11:53
337 查看
class C(object):
a = 'abc'
def fun(self):
print 'this fun'
def __getattribute__(self, *args, **kwargs):
print("__getattribute__() is called")
return object.__getattribute__(self, *args, **kwargs)
def __getattr__(self, name):
print("__getattr__() is called ")
return name + " from getattr"
def __get__(self, instance, owner):
print("__get__() is called", instance, owner)
return self
class C2(object):
d = C()
if __name__ == '__main__':
c = C()
#以下访问存在的属性和方法,会调用__getattribute__
c.fun()
c.a
#访问不存在的属性和方法,会先调用__getattribute__然后调用__getattr__
c.b
#类被引用时调用__get__
c2 = C2()
c2.d
a = 'abc'
def fun(self):
print 'this fun'
def __getattribute__(self, *args, **kwargs):
print("__getattribute__() is called")
return object.__getattribute__(self, *args, **kwargs)
def __getattr__(self, name):
print("__getattr__() is called ")
return name + " from getattr"
def __get__(self, instance, owner):
print("__get__() is called", instance, owner)
return self
class C2(object):
d = C()
if __name__ == '__main__':
c = C()
#以下访问存在的属性和方法,会调用__getattribute__
c.fun()
c.a
#访问不存在的属性和方法,会先调用__getattribute__然后调用__getattr__
c.b
#类被引用时调用__get__
c2 = C2()
c2.d
相关文章推荐
- 飘逸的python - __get__ vs __getattr__ vs __getattribute__以及属性的搜索策略
- python中_get_getattr_getattribute_的差别
- 详解Python中 __get__和__getattr__和__getattribute__的区别
- 详解Python中 __get__和__getattr__和__getattribute__的区别
- 详解Python中 __get__和__getattr__和__getattribute__的区别
- Python开发【笔记】: __get__和__getattr__和__getattribute__区别
- python中__get__,__getattr__,__getattribute__的区别
- python中__get__,__getattr__,__getattribute__的区别
- 飘逸的python - __get__ vs __getattr__ vs __getattribute__以及属性的搜索策略
- python中__get__,__getattr__,__getattribute__的区别
- python中__get__, __getattr__和__getattribute__区别说明
- 转载Python中__getattr__ __getattribute__ __get__解释
- python中__get__,__getattr__,__getattribute__的区别
- python中__get__,__getattr__,__getattribute__的区别
- Python __getattribute__、__getattr__、__get__总结
- python中__get__,__getattr__,__getattribute__ __getitem__的区别
- python中__get__,__getattr__,__getattribute__的区别
- 对比Python中__getattr__和 __getattribute__获取属性的用法
- python3中__get__,__getattr__,__getattribute__的区别
- Python笔记:getattr,__getattr__,__getattribute__