python 类实例化理解
让我们以一个Foo
类开始:
class Foo(object):def __init__(self, x, y=0):self.x = xself.y = y
当你实例化它(即创建该类的一个新的实例)时发生了什么?
f = Foo(1, y=2)
对Foo
的调用到底调用了什么函数或方法呢?大多数新手甚至许多有经验的Python开发者会立刻回答:调用了__init__
方法。如果你停下来仔细想1秒,你会发现这远不是一个正确答案。
__init__
并没有返回一个对象,但是调用Foo(1, y=2)
确实返回了一个对象。而且,__init__
预期一个self
参数,但是当我们调用Foo(1, y=2)
时这里并没有这个参数。这里会有更复杂的工作。在这篇文章中,让我们探究下在Python中实例化一个类时到底发生了什么。
构造顺序
在Python中实例化一个对象包含了几个阶段,但它的妙处在于它们自身是Pythonic(python之禅)的——理解这些步骤使得我们对Python整体有多一点的了解。Foo
是一个类,但是Python中的类也是对象!类、函数、方法以及实例都是对象,并且无论何时你将一对括号放在它们的名字后面时,就会调用它们的__call__
方法。所以Foo(1, y=2)
是等价于Foo.__call__(1, y=2)
的。__call__
方法是定义在Foo
的类中的。Foo
的类是什么呢?
>>> Foo.__class__
<class 'type'>
所以Foo
是类型type
的一个对象并且调用__call__
返回一个Foo
类的对象。让我们看下type
中的__call__
方法是什么样的。这个方法相当的复杂,但是我们尝试尽量简化它。在下面我粘贴了CPython C和PyPy Python的实现。我发想从源码中寻找答案是很有趣的,但是你也可以直接看下面的简化版:
CPython
static PyObject *
type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
{PyObject *obj;if (type->tp_new == NULL) {PyErr_Format(PyExc_TypeError,"cannot create '%.100s' instances",type->tp_name);return NULL;}obj = type->tp_new(type, args, kwds);obj = _Py_CheckFunctionResult((PyObject*)type, obj, NULL);if (obj == NULL)return NULL;/* Ugly exception: when the call was type(something),don't call tp_init on the result. */if (type == &PyType_Type &&PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&(kwds == NULL ||(PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))return obj;/* If the returned object is not an instance of type,it won't be initialized. */if (!PyType_IsSubtype(Py_TYPE(obj), type))return obj;type = Py_TYPE(obj);if (type->tp_init != NULL) {int res = type->tp_init(obj, args, kwds);if (res < 0) {assert(PyErr_Occurred());Py_DECREF(obj);obj = NULL;}else {assert(!PyErr_Occurred());}}return obj;
}
PyPy
def descr_call(self, space, __args__):promote(self)# invoke the __new__ of the typeif not we_are_jitted():# note that the annotator will figure out that self.w_new_function# can only be None if the newshortcut config option is not setw_newfunc = self.w_new_functionelse:# for the JIT it is better to take the slow path because normal lookup# is nicely optimized, but the self.w_new_function attribute is not# known to the JITw_newfunc = Noneif w_newfunc is None:w_newtype, w_newdescr = self.lookup_where('__new__')if w_newdescr is None: # see test_crash_mro_without_object_1raise oefmt(space.w_TypeError, "cannot create '%N' instances",self)w_newfunc = space.get(w_newdescr, self)if (space.config.objspace.std.newshortcut andnot we_are_jitted() andisinstance(w_newtype, W_TypeObject)):self.w_new_function = w_newfuncw_newobject = space.call_obj_args(w_newfunc, self, __args__)call_init = space.isinstance_w(w_newobject, self)# maybe invoke the __init__ of the typeif (call_init and not (space.is_w(self, space.w_type) andnot __args__.keywords and len(__args__.arguments_w) == 1)):w_descr = space.lookup(w_newobject, '__init__')if w_descr is not None: # see test_crash_mro_without_object_2w_result = space.get_and_call_args(w_descr, w_newobject,__args__)if not space.is_w(w_result, space.w_None):raise oefmt(space.w_TypeError,"__init__() should return None")return w_newobject
如果我们忽略错误检查,那么对于常规类的实例化它大致等同如下:
def __call__(obj_type, *args, **kwargs):obj = obj_type.__new__(*args, **kwargs)if obj is not None and issubclass(obj, obj_type):obj.__init__(*args, **kwargs)return obj
__new__
方法为对象分配了内存空间,构建它为一个“空"对象然后__init__
方法被调用来初始化它。
总的来说:
Foo(*args, **kwargs)
等价于Foo.__call__(*args, **kwargs)
- 既然
Foo
是一个type
的实例,Foo.__call__(*args, **kwargs)
实际调用的是type.__call__(Foo, *args, **kwargs)
type.__call__(Foo, *args, **kwargs)
调用type.__new__(Foo, *args, **kwargs)
,然后返回一个对象。obj
随后通过调用obj.__init__(*args, **kwargs)
被初始化。obj
被返回。
定制
现在我们将注意力转移到__new__
方法上。本质上,它是负责实际对象的创建的方法。我们不会具体探究__new__
方法的底层实现细节。它的要点是它会为对象分配空间并返回该对象。有趣的是,一旦你意识到__new__
做了什么,你可以用它来定制有趣的实例创建方式。值得注意的是,尽管__new__
是一个静态方法,但你不需要用@staticmethod
来声明它——它是Python解释器的特例。
一个精彩的展现__new__
方法的力量的例子就是用它来实现一个单例类:
class Singleton(object):_instance = Nonedef __new__(cls, *args, **kwargs):if cls._instance is None:cls._instance = super().__new__(cls, *args, **kwargs)return cls._instance
然后使用它:
>>> s1 = Singleton()
... s2 = Singleton()
... s1 is s2
True
注意在这个单例类的实现中,__init__
方法会在每次我们调用Singleton()
时被调用,所以要小心处理。
另外一个相似的例子是实现Borg design pattern:
class Borg(object):_dict = Nonedef __new__(cls, *args, **kwargs):obj = super().__new__(cls, *args, **kwargs)if cls._dict is None:cls._dict = obj.__dict__else:obj.__dict__ = cls._dictreturn obj
然后:
>>> b1 = Borg()
... b2 = Borg()
... b1 is b2
False
>>> b1.x = 8
... b2.x
8
最后的提醒——上面的例子展示了__new__
的力量,但是只是说明你可以使用它,而不是意味着你应该这么做:
__new__
是Python中最容易被滥用的特性。它晦涩难懂,又缺陷丛生,并且几乎每个用例都被我发现有更好的解决方案(使用其它的Python工具)。但是,当你确实需要__new__
时,它令人难以置信的强大并且值得去理解。
—— Arion Sprague, Python’s Hidden New
在python中,一个问题的最佳解决方案是用__new__
的情况是罕见的。麻烦的是如果你手里有把锤子,任何问题看起来都会像是钉子了 —— 那么你可能会突然遇到很多__new__
能解决的问题。但是我们应该更倾向于更好的设计而不是使用一个全新的工具。__new__
并不总是更好的。
参考
- The Python Language Reference / Data Model
- Eli Bendersky / Python Object Creation Sequence
补充
如果 Foo
定义了一个 __call__
方法,Foo(*args, **kwargs)
并不等于Foo.__call__(*args, **kwargs)
:
>>> class Foo:
... def __call__(self):
... print('running __call__')
...
>>> Foo()
<__main__.Foo object at 0x000000000227ABE0>
>>> Foo.__call__()
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: __call__() missing 1 required positional argument: 'self'
In this case, __call__ is used to call instances of the class :>>> Foo()()
running __call__
>>>
python 类实例化理解相关推荐
- Python类实例化时出现 take no arguements 的错误
Python类实例化时出现 take no arguements 的错误 初学语言往往会在一个简单的地方卡半天,那么类实例化中出现这种问题也是小问题.主要是__init__使用的不对,注意看这个_in ...
- python类实例化_python基础8之类的实例化过程剖析
一.概述 之前我们说关于python中的类,都一脸懵逼,都想说,类这么牛逼到底是什么,什么才是类?下面我们就来讲讲,什么是类?它具有哪些特性. 二.类的语法 2.1 语法 class dog(obje ...
- python 类-如何理解python的类与对象?
挂一个自己的学习笔记 这个时间一长就搞错了,还经常回头来看一看,尤其是self的用法. python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,坐着的椅子就是对象,家里养的小狗也 ...
- python类实例化 输入参数名字 下划线_Python 的类的下划线命名有什么不同?
1,以一个下划线开头的命名 ,如_getFile 2,以两个下划线开头的命名 ,如__filename 3,以两个下划线开头和结尾的命名,如 __init__() 4,其它 这些命名有什么不同吗 首先 ...
- python类实例化 输入参数名字 下划线_Python 用下划线作为变量前缀和后缀指定特殊变量...
原:http://blog.163.com/jackylau_v/blog/static/175754040201182113817834/ 转过来方便查阅=w= Python 用下划线作为变量前缀和 ...
- python 类实例化后作为参数_python--类的实例化
1.类的定义和语法 View Code 2.self关键字 self 这个关键字相当于实例化对象本身(self相当于d),在实例化过程中,把自己传进去了 3.函数__init__() .__del_ ...
- 【Python数据结构】 抽象数据类型 Python类机制和异常
这篇是<数据结构与算法Python语言描述>的笔记,但是大头在Python类机制和面向对象编程的说明上面.我也不知道该放什么分类了..总之之前也没怎么认真接触过基于类而不是独立函数的Pyt ...
- 理解python的类实例化_理解python的类实例化
让我们以一个Foo类开始: class Foo(object): def __init__(self, x, y=0): self.x = x self.y = y 当你实例化它(即创建该类的一个新的 ...
- Python进阶:理解元类创建类ABCMeta
Python进阶:Python进阶:理解元类创建类ABCMeta 一.理解元类(Meta class) 1.1 元类直观理解 1.2 Python官方文档给出的元类描述 二.理解抽象基类(ABC, A ...
最新文章
- 工作流引擎 Activiti 实战系列
- Aysnc的异步执行的线程池
- 金华杭州计算机学校录取分数线,2017年浙江金华各地中考录取分数线
- QGIS简介与源代码编译
- CSS学习——基础分类整理
- FPGA学习之FIFO
- mongoose 更新元素 DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany
- MySQL数据库面试题
- Process 执行shell 脚本
- 中小学计算机教学大纲,中小学信息技术教材教法教学大纲
- java提示框easyui风格_EasyUI 标签框风格(TagBox Style)_Vue EasyUI Demo
- python权限管理系统_Django 自定义权限管理系统详解(通过中间件认证)
- pytorch 中网络参数 weight bias 初始化方法
- stl变易算法(一)
- Java经典设计模式(1):五大创建型模式(附实例和详解)
- Java防御目录穿越漏洞方法_WinRAR目录穿越漏洞复现及防御
- TensorFlow开发者证书 中文手册
- 学习类App原型制作分享-Wokabulary
- 我为App做测试---搜狐新闻(1)
- 应届大学生入职的时候首要问公司的主要两个问题是?