先提示,本文需要一定的python源码基础。许多内容请参考《python源码剖析》。下面切入正题。
今天在群里有人问了一个问题。形如如下的一段程序。
class person:
sum = 0
def __init__(self,name):
self.name=name
person.sum += 1
def __del__(self):
person.sum -= 1
print "%s is leaving" % self.name
a = person('a')
a2 = person('a2')
这段程序的预期的执行结果应该是"a is leaving"和"a2 is leaving"。但是实际上却出乎意料之外,实际的执行结果如下:
a is leaving
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'sum'" in
<bound method person.__del__ of <__main__.person instance at 0x4a18f0>> ignored
为什么引用的名字不同造成的结果会有这么大的差别呢?
分析表面的原因,是person这个引用被指向了None。那他是不是真的是None呢?
def __del__(self):
print globals()['person'] #1
person.sum -= 1
#print "%s is leaving" % self.name
加入红色这行代码,看看是不是真的变成了None。运行结果如下:
__main__.person
None
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'sum'"
in <bound method person.__del__ of <__main__.person instance at 0x4a18c8>> ignored
看来是真的变成了None了。
初步分析原因,应该是程序在执行结束以后,python虚拟机清理环境的时候将"person"这个符号先于"a2"清理了,所以导致在a2的析构函数中无法找到"person"这个符号了。
但是转念一想还是不对,如果是"person"符号找不到了,应该是提示“name 'person' is not defined”才对。说明"person"这个符号还在,那"person"指向的class_object对象还在吗?改变程序为以下格式:
class person:
sum = 0
def __init__(self,name):
self.name=name
person.sum += 1
def __del__(self):
#person.sum -= 1
self.__class__.sum -= 1 #1
#print "%s is leaving" % self.name
a = person('a')
a2 = person('a2')
红色代码就是修改部分,利用自身的__class__来操作。运行结果一切正常。
说明python虚拟机在回收的过程中,只是将"person"这个符号设置成None了。这个结论同时带来2个问题:第一,为什么会设置成None?第二:为什么"person"会先于"a2"而晚于"a"被回收?
先来分析第二个问题。第一反应是不是按照字母的顺序来回收?但是马上这个结论被推翻。"a"和"a2"都在"person"的前面。那么难道是根据globals()这个字典的key顺序来回收?执行一下globals().keys()方法,得到以下结果:
['a', '__builtins__', '__file__', 'person', 'a2', '__name__', '__doc__']
看来的确是这样。
但是为什么是这样?要得出这个结论,看来只有去python源码中找答案了。
大家都知道,python代码在运行的时候,会存在一个frameobject对象来表示运行时的环境。类似于c语言的栈帧,也有点像lisp的函数的生存空间,看起来答案要从frameobject.c这个文件中去找了。
在frameobject.c中发现了一个函数:static void frame_dealloc(PyFrameObject *f)。看来解决问题的关键就在眼前。
在frame_dealloc里面截取了以下一段代码:
Py_XDECREF(f->f_back);
Py_DECREF(f->f_builtins);
Py_DECREF(f->f_globals);
Py_CLEAR(f->f_locals);
Py_CLEAR(f->f_trace);
Py_CLEAR(f->f_exc_type);
Py_CLEAR(f->f_exc_value);
Py_CLEAR(f->f_exc_traceback);
原来减少了引用啊。。关于Py_DECREF这个宏,python源码里面的解释是这样的:
The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
reference counts. Py_DECREF calls the object's deallocator function when
the refcount falls to 0;
这么说来,我们就要去找f_globals的析构函数了。f_globals是个什么呢?当然是PyDictObject了。证据么遍地都是啊,比如随手找了一个,在PyFrameObject * PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,PyObject *locals)这个函数里面有一段代码:
#ifdef Py_DEBUG
if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
(locals != NULL && !PyMapping_Check(locals))) {
PyErr_BadInternalCall();
return NULL;
}
#endif
PyDict_Check。。。检查是否是Dict对象。好吧,此处略过,直接奔向dictobject.c看看里面的代码。
static void
dict_dealloc(register dictobject *mp)
{
register dictentry *ep;
Py_ssize_t fill = mp->ma_fill;
PyObject_GC_UnTrack(mp);
Py_TRASHCAN_SAFE_BEGIN(mp)
for (ep = mp->ma_table; fill > 0; ep++) {
if (ep->me_key) {
--fill;
Py_DECREF(ep->me_key); #
Py_XDECREF(ep->me_value); #仅仅只是引用计数减一
}
}
以下略
哈哈哈。还真是按照key的顺序来一个一个清除的。
不过,怎么又回到了Py_DECREF啊?
看来最终解释这个问题要回到GC上面了。
其实从这个地方也可以看出第一个问题的答案了,为什么是None?
从上面代码可以看出,dictobject对象在析构的时候,仅仅只是将value的引用计数减一,至于这个对象什么时候被真正回收,其实是由GC决定而不确定的。也就是说为什么是None,是因为减一了以后,凑巧GC到了而已。
根据Python本身的文档。
Python 写道
Warning: Due to the precarious circumstances under which __del__() methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to sys.stderr instead. Also, when __del__() is invoked in response to a module being deleted (e.g., when execution of the program is done), other globals referenced by the __del__() method may already have been deleted. For this reason, __del__() methods should do the absolute minimum needed to maintain external invariants. Starting with version 1.5, Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the __del__() method is called.
Python不能保证__del__被调用的时候所有的引用都有,所以,尽量不要overried类的__del__方法。
到此结束。
分享到:
相关推荐
在前面我们讲解了python内置函数locals,内置函数locals直接以字典的形式返回当前位置的所有局部变量,今天需要介绍的是另外一个python内置函数globals,该函数直接以字典的形式返回当前位置的所有全局变量 ...
主要介绍了Python内置函数locals和globals对比,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
本文实例讲述了Python基础教程之内置函数locals()和globals()用法。分享给大家供大家参考,具体如下: 1. 这两个函数主要提供,基于字典的访问局部变量和全局变量的方式。 python 使用叫做名字空间的东西来记录变量...
这篇文章主要介绍了Python globals()和locals()对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Python的两个内置函数,globals()和locals() ,它们...
6.20 *拷贝Python对象、浅拷贝和深拷贝 6.21 序列类型小结 6.22 练习 第7章 映像和集合类型 7.1 映射类型:字典 7.1.1 如何创建字典和给字典赋值 7.1.2 如何访问字典中的值 ...
全局变量中$GLOBALS['']和global的区别
PHP语言中global和$GLOBALS[]的分析 之二,需要的朋友可以参考下
6.20 拷贝Python对象.c浅拷贝和深拷贝 6.21 序列类型小结 6.22 练习 第7章 映像和集合类型 7.1 映射类型:字典 7.1.1 如何创建字典和给字典赋值 7.1.2 如何访问字典中的值 ...
主要用途在于实现命令行-m执行python 模块得效果,但是是在脚本中而不是文件系统上。 runpy一个就两个函数: runpy.``run_module(mod_name, init_globals=None, run_name=None, alter_sys=False) 运行指定模块代码并...
摘要 global 标志实际上是为了提示 ... 注:上面三句话的意思就是,python 解释器发现函数中的某个变量被 global 关键字修饰,就去函数的 __globals__ 字典变量中寻找(因为 python 中函数也是一等对象);同
6.20 *拷贝python对象、浅拷贝和深拷贝 6.21 序列类型小结 6.22 练习 第7章 映像和集合类型 7.1 映射类型:字典 7.1.1 如何创建字典和给字典赋值 7.1.2 如何访问字典中的值 ...
6.20 *拷贝python对象、浅拷贝和深拷贝 6.21 序列类型小结 6.22 练习 第7章 映像和集合类型 7.1 映射类型:字典 7.1.1 如何创建字典和给字典赋值 7.1.2 如何访问字典中的值 ...
主要介绍了Python两个内置函数 locals 和globals(学习笔记),需要的朋友可以参考下
前端开源库-gulp-babel-globalsGulp Babel Globals,用于Babel Globals的Gulp插件。
Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In ...
前端开源库-babel-globalsbabel globals,编译javascript文件及其与babel的所有依赖项。
前端开源库-babel-plugin-globalsbabel plugin globals是一个babel插件,它向全局变量公开ES6模块。
如果你已经学习了包,模块这些知识了。 你会不会有好奇:Python为什么可以直接...>>> globals() {'__builtins__': , '__name__': '__main__', '__doc__': None, '__package__': None} 你可以再次导入 __builtin__(No