python没有运行_Python没有执行__init__
疑惑 提出問題
前天同事問我一個問題,為什么這個腳本中的沒有調用A 的__init__。腳本如下:
1 classA(object):2 def __init__(self, *args, **kwargs):3 print "Call init from %s" %self.__class__
4
5 def __new__(cls, *args, **kwargs):6 obj = object.__new__(cls, *args, **kwargs)7 print "Call new from %s" %obj.__class__
8 returnobj9
10
11 classB(object):12 def __init__(self, *args, **kwargs):13 print "Call init from %s" %self.__class__
14
15 def __new__(cls, *args, **kwargs):16 obj = object.__new__(A, *args, **kwargs)17 print "Call new from %s" %obj.__class__
18 returnobj19
20 b = B()
其實我也比較奇怪,這個腳本寫的比較奇怪,class B的的__new__返回了A的實例。也只是只執(zhí)行了B的__new__方法,并沒有執(zhí)行A的__init__方法。
深入 迷失
遇到這個問題:
要深入首先查看了一下代碼的編譯后的python指令,查看B,是B的__init__方法的指令,
如上圖,為了具體查看B()方法是如何調用,就添加了一個方法test_B。B()顯示僅僅是兩條指令
LOAD_GLOBAL
CALL_FUNCTION
查看Python源代碼,到
PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
{
ternaryfunc call;
if ((call = func->ob_type->tp_call) != NULL) {
PyObject *result;
if (Py_EnterRecursiveCall(" while calling a Python object"))
return NULL;
result = (*call)(func, arg, kw);
**************看到這里的時候有點迷失了,不知道tp_call是個什么東西了,不確定由typeobject來操作
(這個必須進行檢討因為tp_call已已經明確了他來自哪里)***************
調試 問題解決
最后使出了,大招對Python源代碼進行調試,在Linux上編譯python源代碼加上參數--with-debug, 然后執(zhí)行gdb -ex r --args python test.py,
在call_function,do_call,PyObject_Call 執(zhí)行到之后,打上斷點。看到他運行到了
type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
obj = type->tp_new(type, args, kwds);
if (obj != NULL) {
if (!PyType_IsSubtype(obj->ob_type, type))
return obj;
,這個時候我再去看代碼。發(fā)現哪里已經寫好注釋了:
/* If the returned object is not an instance of type,
it won't be initialized. */
好吧,很明確了了。沒有產生和class類型一致的instance,就不會進行初始化。即調用__init__。
問題解決了。。也學習了
要問我怎么知道在那個地方打斷點,因為我已經看過了代碼,只是理解沒有那么深。
總結
以上是生活随笔為你收集整理的python没有运行_Python没有执行__init__的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python getopts_linux
- 下一篇: spring 三级缓存_通过画图+视频把