本来以为自己对__getattribute__已经比较了解了,结果用到的时候,才发现有一些知识点之前一直没有真正弄明白,记录如下(针对python3,python2差异较大):

  1. object类有__getattribute__属性,因此所有的类默认就有__getattribute__属性(所有类都继承自object),object的__getattribute__起什么用呢?它做的就是查找自定义类的属性,如果属性存在则返回属性的值,如果不存在则抛出AttributeError。

    还是看代码:
>>> class A:
def __getattribute__(self, name):
print('in A')
return super().__getattribute__(name) >>> class B(A):
def spam(self):
print('spam') >>> b = B()
>>> in A
b.spam()
in A
spam

当在命令行输入b.spam(,甚至括号还没有输入完整的时候,就已经触发了__getattribute__。可见,当实例在查找任何属性的时候(注意是实例,这是另一个知识点,后面再详谈),都会触发__getattribute__方法。

基于此特性,可以方便的使用类装饰器为其所有属性(不管是有的还是没有的)扩充功能,例如,加一个log记录功能,代码如下:

>>> def log_attr(cls):
cls_getattribute = cls.__getattribute__
def new_getattribute(self, name):
print('catch name')
return cls_getattribute(self, name)
cls.__getattribute__ = new_getattribute
return cls >>> @log_attr
class C:
def __init__(self, x):
self.x = x
def spam(self):
print('spam') >>> c = C(42)
>>> c.x
catch name
42
>>> catch name
c.spam()
catch name
spam
>>> c.y
catch name
Traceback (most recent call last):
File "<pyshell#83>", line 1, in <module>
c.y
File "<pyshell#77>", line 5, in new_getattribute
return cls_getattribute(self, name)
AttributeError: 'C' object has no attribute 'y'
  1. 只有在实例的命名空间查找属性的时候,才会触发__getattribute__,在类的命名空间中查找,是不会触发__getattribute__的。

    还是第一个例子:
>>> B.spam
<function B.spam at 0x000001D3759646A8>
>>> b = B()
>>> B.spam(b)
spam

可见,当类直接调用spam方法的时候,不会触发__getattribute__,而且当以B.spam(b)形式调用的时候,巧妙的绕过了__getattribute__。

接下来,是一个重要的知识点,即特殊的方法,如__len__,__str__等等操作符重载方法,当隐式调用的时候,在python3中会直接在类的命名空间中查找,因此是不会触发__getattribute__的!!如下:

>>> class C:
def __len__(self):
return 42
def __getattribute__(self, name):
print(f'catch {name}')
return super().__getattribute__(name) >>> c = C()
>>> len(c)
42

but,当直接显示调用的时候,python不会将其当作啥特殊方法,仍然会从实例的命名空间查找,此时,就会触发__getattribute__:

>>> c.__len__()
catch __len__
42

最大的影响就是,在委托类型的代码中,操作符重载的方法代码得重新写,如下:

>>> class A:
def __len__(self):
return 42
def attr1(self):
print('attr1') >>> class B:
def __init__(self):
self.a = A()
def __getattribute__(self, name):
if name == 'a':
return super().__getattribute__(name)
else:
return getattr(self.a, name) >>> b.attr1()
attr1
>>> len(b)
Traceback (most recent call last):
File "<pyshell#170>", line 1, in <module>
len(b)
TypeError: object of type 'B' has no len()

可见,attr1正确的被代理,但是len方法没有被代理,因为隐式调用的时候,直接在B类的命名空间查找__len__方法,根本就没有触发__getattribute__。如果显示调用,就可以被代理了。

>>> b.__len__()
42

最后,还有一个坑需要注意,如上例B类的__getattribute__方法中,判断一下a是否B自身的实例属性的代码不可少,是则调用父类的__getattribute__方法返回正确的self.a,否则在getattr(self.a, name)中,self.a会不断的触发__getattribute__,从而会陷入无限循环。

对了,最后还有一小点,有一个比较特殊的操作符重载方法,即dir,因为它会从实例的命名空间开始查找__dict__和__class__特殊方法,因此也会触发__getattribute__。

貌似关于__getattribute__的知识点就这些了,以上均为个人原创,平时学习的积累,打字不易,转载还请注明出处。

Python的__getattribute__二三事的更多相关文章

  1. Python中字符串二三事

    首先说两个运算符: " == " 运算符测试值的等价性,递归地比较所有内嵌对象 " is " 表达式测试对象的同一性,测试两者是否为同一对象(是否为同一地址) ...

  2. python basemap readshapefile二三事

    今天要用到basemap读取shp文件报错,查了很多资料,都没有解决. 先是: fig,ax = plt.subplots(figsize=(15,10)) from mpl_toolkits.bas ...

  3. python基础---- __getattribute__----__str__,__repr__,__format__----__doc__----__module__和__class__

    目录: 一. __getattribute__ 二.__str__,__repr__,__format__ 三.__doc__ 四.__module__和__class__ 一. __getattri ...

  4. python排序之二冒泡排序法

    python排序之二冒泡排序法 如果你理解之前的插入排序法那冒泡排序法就很容易理解,冒泡排序是两个两个以向后位移的方式比较大小在互换的过程好了不多了先上代码吧如下: 首先还是一个无序列表lis,老规矩 ...

  5. Python 基础语法(二)

    Python 基础语法(二) --------------------------------------------接 Python 基础语法(一) ------------------------ ...

  6. Java并发编程二三事

    Java并发编程二三事 转自我的Github 近日重新翻了一下<Java Concurrency in Practice>故以此文记之. 我觉得Java的并发可以从下面三个点去理解: * ...

  7. linux杂记(十二?) 关于账号和密码的二三事

    关于密码的二三事 关于账号和密码的二三事 久了不更linux的相关知识,实在是懒得想内容点(纯粹是懒).那么今天就来谈谈关于linux密码和账号的重要概念. 假如你的主机遭到入侵,那么对方的第一个侵入 ...

  8. Python 数据分析(二 本实验将学习利用 Python 数据聚合与分组运算,时间序列,金融与经济数据应用等相关知识

    Python 数据分析(二) 本实验将学习利用 Python 数据聚合与分组运算,时间序列,金融与经济数据应用等相关知识 第1节 groupby 技术 第2节 数据聚合 第3节 分组级运算和转换 第4 ...

  9. 初学 Python(十二)——高阶函数

    初学 Python(十二)--高阶函数 初学 Python,主要整理一些学习到的知识点,这次是高阶函数. #-*- coding:utf-8 -*- ''''' 话说高阶函数: 能用函数作为参数的函数 ...

随机推荐

  1. Mysql配置文件详解 my.cof

    Mysql配置文件详解 # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/ ...

  2. 276. Paint Fence篱笆涂色

    [抄题]: There is a fence with n posts, each post can be painted with one of the k colors. You have to ...

  3. code1091 传染病控制

    1.读入图,边是双向的 2.递归建树,同时确定每一层的节点 3.dfs按层搜索,先把这一层所有被传染的(die[pa[k]]=true的)的die置为true 然后循环,每次把一个die为true的变 ...

  4. cakephp中sql查询between

    $trading_list = $this->Trading->find('all', array('conditions' => array('buy_time BETWEEN ? ...

  5. Part2_lesson2---ARM处理器工作模式

    arm公司发布的学习手册:ARM Architecture Reference Manual. 打开之: 找到Programmers' Model->A2.2 Processor modes. ...

  6. springmvc框架简单搭建

    一.利用xml 配置 1.web.xml   <web-app version="2.4"     xmlns="http://java.sun.com/xml/n ...

  7. 编写高质量代码改善C#程序的157个建议——建议113:声明变量前考虑最大值

    建议113:声明变量前考虑最大值 假设正在开发一个工资系统,其中一个模块负责处理加薪.代码如下: static void Main(string[] args) { ; salary = (); Co ...

  8. mybatis SqlMapConfig.xml

    一.SqlMapConfig.xml 1.属性properties 在入门时,以抽取出连接数据库的属性得到properties文件. a.可以通过resource和url来获得属性. b.proper ...

  9. 解决word2013老是打开未响应情况

    问题:自己装了word2013时,每次打开word或者工作时,老是出现一个圈圈,提示未反应,是否关闭程序这样的提示: 解决方法:文件->选项->高级->显示->禁用硬件加速,将 ...

  10. What is the difference between inverse converse and reverse?

    http://wiki.answers.com/Q/What_is_the_difference_between_inverse_converse_and_reverse First, it help ...