对特殊方法的访问 - Special method lookup
对特殊方法的访问 - 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的更多相关文章
- action 方法的访问
Action中的方法的访问: 访问Action的中的方法,默认情况下只能访问execute方法.那么多次请求就不能提交到一个Action.能不能一个模块的多次请求提交到一个Action中? * 需要使 ...
- 【译文】 C#面向对象的基本概念 (Basic C# OOP Concept) 第一部分(类,对象,变量,方法,访问修饰符)
译文出处:http://www.codeproject.com/Articles/838365/Basic-Csharp-OOP-Concept 相关文档:http://files.cnblogs.c ...
- .NET设计模式(5):工厂方法模式(Factory Method)(转)
工厂方法模式(Factory Method) ——.NET设计模式系列之五 Terrylee,2004年1月2日 概述 在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实 ...
- .NET设计模式(5):工厂方法模式(Factory Method)
):工厂方法模式(Factory Method) 工厂方法模式(Factory Method) --.NET设计模式系列之五 Terrylee,2004年1月2日 转载:http://terry ...
- 24种设计模式--模版方法模式【Template Method Pattern】
周三,9:00,我刚刚坐到位置,打开电脑准备开始干活.“小三,小三,叫一下其它同事,到会议室,开会”老大跑过来吼,带着淫笑.还不等大家坐稳,老大就开讲了,“告诉大家一个好消息,昨天终于把牛叉模型公司的 ...
- 工厂方法模式(Factory Method)和抽象工厂模式(Abstact Factory)
分类 工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的.工厂模式在<Java 与模式>中分为三类:1)简单工厂模式(Simple Facto ...
- NET设计模式 第二部分 创建型模式(4):工厂方法模式(Factory Method)
工厂方法模式(Factory Method) ——.NET设计模式系列之五 Terrylee,2004年1月2日 概述 在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实 ...
- java中,方法可以访问他的类对象的任何私有特性
java中,方法可以访问他的类对象的任何私有特性 读一本书(Core Java for the Impatient)时,发现这个注意,以前的时候没有在意,今天仔细想想发现记忆不深刻.记录一下 下面代码 ...
- [转载]Python方法绑定——Unbound/Bound method object的一些梳理
本篇主要总结Python中绑定方法对象(Bound method object)和未绑定方法对象(Unboud method object)的区别和联系.主要目的是分清楚这两个极容易混淆的概念,顺便将 ...
随机推荐
- Java项目之客户信息管理软件
模拟实现基于文本界面的客户信息管理软件,该软件能够实现对客户对象的插入. 修改和删除(用数组实现),并能够打印客户明细表. 项目采用分级菜单方式.主菜单如下: “添加客户”的界面及操作过程如下所示: ...
- sin 与 cos 的用法
这两个函数使用过程中要,如 sin(x) , x 在这里表示的是弧度,至于弧度要如何计算呢 ? pi / 180 = 弧度 / 角度 内部的参数可以整数或者浮点数,以前面做过的一道题而言,大量的调用函 ...
- 小白学Java:奇怪的RandomAccess
目录 小白学Java:奇怪的RandomAccess RandomAccess是个啥 forLoop与Iterator的区别 判断是否为RandomAccess 小白学Java:奇怪的RandomAc ...
- 2.Scanner的进阶使用
package com.duan.scanner; import java.util.Scanner; public class Demo04 { public static void main(St ...
- 代码审计之CVE-2018-7600-Drupal远程代码执行漏洞-Render API
今天学习一下Drupal的另一个漏洞,由于渲染数组不当造成的漏洞 poc: url:http://localhost/drupal-8.5.0/user/register?element_parent ...
- Bootstrap Table的使用详解
Bootstrap Table是基于 Bootstrap 的 jQuery 表格插件,通过简单的设置,就可以拥有强大的单选.多选.排序.分页,以及编辑.导出.过滤(扩展)等等的功能.接下来我们来介绍B ...
- 【WPF学习】第十三章 理解路由事件
每个.NET开发人员都熟悉“事件”的思想——当有意义的事情发生时,由对象(如WPF元素)发送的用于通知代码的消息.WPF通过事件路由(event routing)的概念增强了.NET事件模型.事件路由 ...
- if(a)是什么意思
if(a)等价于 if(a!=0) if(!a)等价于 if(a==0)
- if循环后打印数据竖行输出和横排输出
代码A如下: def func(*kargs): return kargs, l = func(5,3,4,5,6) for i in l: print (i) 打印结果: ...
- Dart语言学习(九) 运算符
一.运算符及其描述 二.Dart运算符注意点 1. 除法运算符"/" 和 整除运算法"~/" 的区别 除法运算符 "/" 结果是浮点型 整 ...