问题是类包含回自身的循环引用 – 所以当它们被删除时它们不容易收集 – 因此__del__方法odf不调用元类.
我可以使用Pypy的Python实现触发它,但不使用cpython – 2.6或3.2.甚至要触发它,我不得不手动调用垃圾收集器 –
众所周知,程序出口处的Python环境充满了不一致性,并且在类的足够内部信息存在以允许sae关闭的情况下调用__del__方法的可能性非常小.
这是我的Pypy会话,我确实触发了对类’__del__的调用
~]$pypy
Python 2.5.2 (78826, Nov 29 2010, 00:18:05)
[PyPy 1.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``sorry, I'll teach the cactus how
to swim later''
>>>> import gc
>>>> class Meta(type):
.... def __del__(cls):
.... print ("Arghh!!")
....
>>>> class A(object):
.... __metaclass__ = Meta
....
>>>> del A
>>>> gc.collect()
Arghh!!
0
>>>>