英文原文來自Understanding Python Class Instantiation
從PythonWeekly郵件中看到
讓我們以一個Foo
類開始:
class Foo(object):
def __init__(self, x, y=0):
self.x = x
self.y = y
當你實例化它(即創(chuàng)建該類的一個新的實例)時發(fā)生了什么迫像?
f = Foo(1, y=2)
對Foo
的調(diào)用到底調(diào)用了什么函數(shù)或方法呢劈愚?大多數(shù)新手甚至許多有經(jīng)驗的Python開發(fā)者會立刻回答:調(diào)用了__init__
方法。如果你停下來仔細想1秒闻妓,你會發(fā)現(xiàn)這遠不是一個正確答案菌羽。
__init__
并沒有返回一個對象,但是調(diào)用Foo(1, y=2)
確實返回了一個對象由缆。而且注祖,__init__
預(yù)期一個self
參數(shù)猾蒂,但是當我們調(diào)用Foo(1, y=2)
時這里并沒有這個參數(shù)。這里會有更復(fù)雜的工作是晨。在這篇文章中肚菠,讓我們探究下在Python中實例化一個類時到底發(fā)生了什么。
構(gòu)造順序
在Python中實例化一個對象包含了幾個階段罩缴,但它的妙處在于它們自身是Pythonic(python之禪)的——理解這些步驟使得我們對Python整體有多一點的了解蚊逢。Foo
是一個類,但是Python中的類也是對象箫章!類烙荷、函數(shù)、方法以及實例都是對象檬寂,并且無論何時你將一對括號放在它們的名字后面時终抽,就會調(diào)用它們的__call__
方法。所以Foo(1, y=2)
是等價于Foo.__call__(1, y=2)
的焰薄。__call__
方法是定義在Foo
的類中的拿诸。Foo
的類是什么呢?
>>> Foo.__class__
<class 'type'>
所以Foo
是類型type
的一個對象并且調(diào)用__call__
返回一個Foo
類的對象塞茅。讓我們看下type
中的__call__
方法是什么樣的亩码。這個方法相當?shù)膹?fù)雜,但是我們嘗試盡量簡化它野瘦。在下面我粘貼了CPython C和PyPy Python的實現(xiàn)描沟。我發(fā)想從源碼中尋找答案是很有趣的,但是你也可以直接看下面的簡化版:
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
如果我們忽略錯誤檢查鞭光,那么對于常規(guī)類的實例化它大致等同如下:
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__
方法為對象分配了內(nèi)存空間吏廉,構(gòu)建它為一個“空"對象然后__init__
方法被調(diào)用來初始化它。
總的來說:
-
Foo(*args, **kwargs)
等價于Foo.__call__(*args, **kwargs)
- 既然
Foo
是一個type
的實例惰许,Foo.__call__(*args, **kwargs)
實際調(diào)用的是type.__call__(Foo, *args, **kwargs)
-
type.__call__(Foo, *args, **kwargs)
調(diào)用type.__new__(Foo, *args, **kwargs)
席覆,然后返回一個對象。 -
obj
隨后通過調(diào)用obj.__init__(*args, **kwargs)
被初始化。 -
obj
被返回。
定制
現(xiàn)在我們將注意力轉(zhuǎn)移到__new__
方法上搏熄。本質(zhì)上,它是負責實際對象的創(chuàng)建的方法生巡。我們不會具體探究__new__
方法的底層實現(xiàn)細節(jié)。它的要點是它會為對象分配空間并返回該對象见妒。有趣的是孤荣,一旦你意識到__new__
做了什么,你可以用它來定制有趣的實例創(chuàng)建方式。值得注意的是盐股,盡管__new__
是一個靜態(tài)方法钱豁,但你不需要用@staticmethod
來聲明它——它是Python解釋器的特例。
一個精彩的展現(xiàn)__new__
方法的力量的例子就是用它來實現(xiàn)一個單例類:
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
注意在這個單例類的實現(xiàn)中遂庄,__init__
方法會在每次我們調(diào)用Singleton()
時被調(diào)用寥院,所以要小心處理劲赠。
另外一個相似的例子是實現(xiàn)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
False
>>> b1.x = 8
... b2.x
8
最后的提醒——上面的例子展示了__new__
的力量涛目,但是只是說明你可以使用它,而不是意味著你應(yīng)該這么做:
__new__
是Python中最容易被濫用的特性凛澎。它晦澀難懂霹肝,又缺陷叢生,并且?guī)缀趺總€用例都被我發(fā)現(xiàn)有更好的解決方案(使用其它的Python工具)塑煎。但是沫换,當你確實需要__new__
時,它令人難以置信的強大并且值得去理解最铁。
—— Arion Sprague, Python’s Hidden New
在python中讯赏,一個問題的最佳解決方案是用__new__
的情況是罕見的。麻煩的是如果你手里有把錘子冷尉,任何問題看起來都會像是釘子了 —— 那么你可能會突然遇到很多__new__
能解決的問題漱挎。但是我們應(yīng)該更傾向于更好的設(shè)計而不是使用一個全新的工具。__new__
并不總是更好的雀哨。
參考
補充
如果 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__
>>>