__del__ PyPy和CPython的不同点 动态编译(注意不是解释) 析构函数被调用的次数
小结
1、cpy的垃圾回收会对调用__del__多次;pypy仅仅一次;
https://www.liaoxuefeng.com/wiki/1016959663602400/1016966024263840
PyPy是另一个Python解释器,它的目标是执行速度。PyPy采用JIT技术,对Python代码进行动态编译(注意不是解释),所以可以显著提高Python代码的执行速度。
绝大部分Python代码都可以在PyPy下运行,但是PyPy和CPython有一些是不同的,这就导致相同的Python代码在两种解释器下执行可能会有不同的结果。如果你的代码要放到PyPy下执行,就需要了解PyPy和CPython的不同点。
https://pypy.readthedocs.io/en/latest/cpython_differences.html
There are a few extra implications from the difference in the GC. Most notably, if an object has a __del__
, the __del__
is never called more than once in PyPy; but CPython will call the same __del__
several times if the object is resurrected and dies again (at least it is reliably so in older CPythons; newer CPythons try to call destructors not more than once, but there are counter-examples). The __del__
methods are called in “the right” order if they are on objects pointing to each other, as in CPython, but unlike CPython, if there is a dead cycle of objects referencing each other, their __del__
methods are called anyway; CPython would instead put them into the list garbage
of the gc
module. More information is available on the blog [1] [2].