[转]Python(12) 理解python的类实例化
来源:互联网 发布:永恒战士2无双战神mac 编辑:程序博客网 时间:2024/06/14 23:45
作者:treelake
链接:http://www.jianshu.com/p/f63ad9d550f1
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
侵删。
让我们以一个Foo类开始:
class Foo(object): def __init__(self, x, y=0): self.x = x self.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 type if 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 set w_newfunc = self.w_new_function else: # 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 JIT w_newfunc = None if w_newfunc is None: w_newtype, w_newdescr = self.lookup_where('__new__') if w_newdescr is None: # see test_crash_mro_without_object_1 raise oefmt(space.w_TypeError, "cannot create '%N' instances", self) w_newfunc = space.get(w_newdescr, self) if (space.config.objspace.std.newshortcut and not we_are_jitted() and isinstance(w_newtype, W_TypeObject)): self.w_new_function = w_newfunc w_newobject = space.call_obj_args(w_newfunc, self, __args__) call_init = space.isinstance_w(w_newobject, self) # maybe invoke the __init__ of the type if (call_init and not (space.is_w(self, space.w_type) and not __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_2 w_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 = None def __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 = None def __new__(cls, *args, **kwargs): obj = super().__new__(cls, *args, **kwargs) if cls._dict is None: cls._dict = obj.__dict__ else: obj.__dict__ = cls._dict return obj
然后:
b1 = Borg()
… b2 = Borg()
… b1 is b2
Falseb1.x = 8
… b2.x
8
最后的提醒——上面的例子展示了new的力量,但是只是说明你可以使用它,而不是意味着你应该这么做:
new是Python中最容易被滥用的特性。它晦涩难懂,又缺陷丛生,并且几乎每个用例都被我发现有更好的解决方案(使用其它的Python工具)。但是,当你确实需要new时,它令人难以置信的强大并且值得去理解。
—— Arion Sprague, Python’s Hidden New
在python中,一个问题的最佳解决方案是用new的情况是罕见的。麻烦的是如果你手里有把锤子,任何问题看起来都会像是钉子了 —— 那么你可能会突然遇到很多new能解决的问题。但是我们应该更倾向于更好的设计而不是使用一个全新的工具。new并不总是更好的。
- [转]Python(12) 理解python的类实例化
- 理解python的类实例化
- python的多线程简单实例理解
- Python类的理解
- python 类的理解
- Python 类实例化时变换实例的类
- Python 类属性的理解
- Python元类的理解
- python中元类的理解
- Python中元类的理解
- python 元类的理解
- python中的__call__的个人理解(wsgi实例解释)
- python中类实例化[?]
- 简单理解Python中的类对象、实例对象、属性、方法
- python的实例应用
- python的多线程实例
- python的MD5实例
- PYTHON编码的理解
- Gson JsonParser
- Bean注入[笔记]
- Linux内存管理
- Java 数组 内存(栈和堆)
- $(document).ready()和$(window).load()区别
- [转]Python(12) 理解python的类实例化
- 实现关联表查询(转载自孤傲苍狼)
- STM32F103 GPIO输出模式2MHz,10MHz,50MHz波形对比
- 数据结构实验之图论九:最小生成树
- Eclipse快捷键(用到就写下来)
- js时间戳转化为便准时间格式
- PyTorch学习总结(七)——自动求导机制
- 用 Python 写一个 NoSQL 数据库
- 工厂模式