对特殊方法的访问 - Special method lookup

对于用户自定义的 class 来说, 特殊方法只有通过定义对象的类型object’s type (而非通过 instance
的 __dict__属性)被定义, 才能保证特殊方法的隐式调用.
也就是说给自定义的 class 打 特殊方法的 monkey patching 后,再通过 conventional'传统方式' 调用是行不通的.
注, conventional'传统方式' 调用 指的是如下面例子中 len() 跟 __len__() 的对应关系.
见下例的 exception,
例,
>>> class C:
... pass
...
>>> c = C()
>>> c.__len__ = lambda: 5
>>> c.__len__()
5
>>> len(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'C' has no len() 报错的原因在于, 所有对象(包括类型对象) 都有实现特殊方法, 如果通过'传统方式'隐式调用
这些特殊方法(len(c)), 当在类型对象本身上调用对应的特殊方法的时候便会失败.
详见 python doc 中的描述,
The rationale behind this behaviour lies with a number of special methods such as __hash__() and __repr__()
that are implemented by all objects, including type objects.
If the implicit lookup of these methods used the conventional lookup process,
they would fail when invoked on the type object itself: 例如,
>>> 1 .__hash__() == hash(1)
True
>>> int.__hash__() == hash(int)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor '__hash__' of 'int' object needs an argument 错误在于, 试图去调用一个类(int) 的 unbound method(__hash__), 有时这样的错误调用行为
被称作‘metaclass confusion’. 可以采取通过 instance 访问 special methods
的方式绕过 ‘metaclass confusion’.
>>> type(1).__hash__(1) == hash(1)
True
>>> type(int).__hash__(int) == hash(int)
True 这样做的另外的一个好处是, 对特殊方法的隐式调用同时也绕过 __getattribute__ 方法,
对该对象的的 metaclass 来说也是这样.
>>> class Meta(type):
... def __getattribute__(*args):
... print("Metaclass getattribute invoked")
... return type.__getattribute__(*args)
...
>>> class C(object, metaclass=Meta):
... def __len__(self):
... return 10
... def __getattribute__(*args):
... print("Class getattribute invoked")
... return object.__getattribute__(*args)
...
>>> c = C()
>>> c.__len__() # Explicit lookup via instance
Class getattribute invoked
10
>>> type(c).__len__(c) # Explicit lookup via type
Metaclass getattribute invoked
10
>>> len(c) # Implicit lookup
10 summarize,
'绕过' __getattribute__ 的机制最佳化了解释器的解释速度, 而这种机制是通过对特殊方法的灵活处理
实现的,即特殊方法必须在类对象本身上 set, 而不能通过 monkey patching,再通过 conventional'传统方式' 调用. 注,
conventional'传统方式' 调用 指的是诸如例子中 len() 跟 __len__() 的对应关系. reference,
python doc,
https://docs.python.org/3/reference/datamodel.html#special-lookup

对特殊方法的访问 - Special method lookup的更多相关文章

  1. action 方法的访问

    Action中的方法的访问: 访问Action的中的方法,默认情况下只能访问execute方法.那么多次请求就不能提交到一个Action.能不能一个模块的多次请求提交到一个Action中? * 需要使 ...

  2. 【译文】 C#面向对象的基本概念 (Basic C# OOP Concept) 第一部分(类,对象,变量,方法,访问修饰符)

    译文出处:http://www.codeproject.com/Articles/838365/Basic-Csharp-OOP-Concept 相关文档:http://files.cnblogs.c ...

  3. .NET设计模式(5):工厂方法模式(Factory Method)(转)

    工厂方法模式(Factory Method) ——.NET设计模式系列之五 Terrylee,2004年1月2日 概述 在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实 ...

  4. .NET设计模式(5):工厂方法模式(Factory Method)

    ):工厂方法模式(Factory Method)    工厂方法模式(Factory Method) --.NET设计模式系列之五 Terrylee,2004年1月2日 转载:http://terry ...

  5. 24种设计模式--模版方法模式【Template Method Pattern】

    周三,9:00,我刚刚坐到位置,打开电脑准备开始干活.“小三,小三,叫一下其它同事,到会议室,开会”老大跑过来吼,带着淫笑.还不等大家坐稳,老大就开讲了,“告诉大家一个好消息,昨天终于把牛叉模型公司的 ...

  6. 工厂方法模式(Factory Method)和抽象工厂模式(Abstact Factory)

    分类 工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的.工厂模式在<Java 与模式>中分为三类:1)简单工厂模式(Simple Facto ...

  7. NET设计模式 第二部分 创建型模式(4):工厂方法模式(Factory Method)

    工厂方法模式(Factory Method) ——.NET设计模式系列之五 Terrylee,2004年1月2日 概述 在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实 ...

  8. java中,方法可以访问他的类对象的任何私有特性

    java中,方法可以访问他的类对象的任何私有特性 读一本书(Core Java for the Impatient)时,发现这个注意,以前的时候没有在意,今天仔细想想发现记忆不深刻.记录一下 下面代码 ...

  9. [转载]Python方法绑定——Unbound/Bound method object的一些梳理

    本篇主要总结Python中绑定方法对象(Bound method object)和未绑定方法对象(Unboud method object)的区别和联系.主要目的是分清楚这两个极容易混淆的概念,顺便将 ...

随机推荐

  1. Spring学习记录6——ThreadLocal简介

    Spring通过各种模板类降低了开发者使用各种数据持久化技术的难度.这些模板类是线程安全的,所以 多个DAO可以复用同一个模板实例而不会发生冲突.在使用模板类访问底层数据时,模板类需要绑定数据连接或者 ...

  2. cf - 920 c 求能否实现交换

    C. Swap Adjacent Elements time limit per test 1 second memory limit per test 256 megabytes input sta ...

  3. TCP/IP协议与HTTP协议(二)

    TCP/IP协议是传输层协议,主要解决数据如何在网络中传输,而HTTP是应用层协议,主要解决如何包装数据. 1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过 ...

  4. struts2 convention插件

    1.struts2自2.1以后推荐使用Convention Plugin支持struts零配置支持(引入jar:struts2-convention-plugin-2.x.x.jar)①convent ...

  5. Docker学习(十一)Docker系列结束-新的开始K8S

    Docker学习(十一)Docker系列结束-新的开始K8S 标签(空格分隔): docke k8s Docker系列结束 上一篇讲到使用docker官方提供的容器编排工具docker-compose ...

  6. 2020寒假学习01 Scala 编程初级实践

    1. 计算级数请用脚本的方式编程计算并输出下列级数的前 n 项之和 Sn,直到 Sn 刚好大于或等于 q为止,其中 q 为大于 0 的整数,其值通过键盘输入. Sn = 2/1+3/2+4/3+... ...

  7. [ Python入门教程 ] Python中JSON模块基本使用方法

    JSON (JavaScript Object Notation)是一种使用广泛的轻量数据格式,Python标准库中的json模块提供了一种简单的方法来编码和解码JSON格式的数据.用于完成字符串和p ...

  8. BERT模型总结

    BERT模型总结 前言 ​ BERT是在Google论文<BERT: Pre-training of Deep Bidirectional Transformers for Language U ...

  9. AOP编程实践总结

    AOP编程实践总结 AOP概述 AOP(Aspect-Oriented Programming,面向方面编程)是OOP(Object-Oriented Programing,面向对象编程)的补充和完善 ...

  10. Nginx(3)---代理与负载均衡

    一.代理简述 代理分为正向代理和反向代理, 正向代理:客户端与目标服务器之间增加一个代理服务器,客户端直接访问代理服务器,在由代理服务器访问目标服务器并返回客户端并返回 .比如夜深人静的时候访问的一些 ...