问题是类包含回自身的循环引用 - 因此当它们被删除时它们不容易收集 - 因此不调用元类的del方法。
我可以使用Pypy的Python实现触发它,但不使用cpython - 2.6或3.2。甚至要触发它,我不得不手动调用垃圾收集器 -
众所周知,程序出口处的Python环境充满了不一致性,并且调用del方法的可能性,同时存在足够的类内部信息以允许sae关闭将非常渺茫。
这是我的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
>>>>