使用del v,从范围中删除变量名,使用或从集合中删除对象,或使用del v[item]或del[i:j]从属性中删除属性del v.name,或通过其他任何方式删除对对象的引用,都不会触发任何析构函数调用或释放任何内存本身。对象仅在其引用计数达到零时才会被破坏。
>>> import gc
>>> gc.disable() # 禁用垃圾收集器
>>> class Track:
def __init__(self):
print("Initialized")
def __del__(self):
print("Destructed")
>>> def bar():
return Track()
>>> t = bar()
Initialized
>>> another_t = t # 分配另一个参考
>>> print("...")
...
>>> del t # 尚未销毁-another_t仍引用它
>>> del another_t # 最终引用不见了,对象被破坏了
Destructed