__del__ is not called after creating a new reference

Steve D'Aprano steve+python at pearwood.info
Mon Mar 20 13:51:48 EDT 2017


On Sat, 18 Mar 2017 01:54 am, Oleg Nesterov wrote:

[...]
> However, this trivial test-case
> 
> class C:
> def __del__(self):
> print("DEL")
> global X
> X = self
> C()
> print(X)
> X = 0
> print(X)
> 
> shows that __del__ is called only once, it is not called again after "X =
> 0":
> 
> DEL
> <__main__.C object at 0x7f067695f4a8>
> 0

I cannot confirm that test case. When I try it, I get NameError:


py> class C:
...     def __del__(self):
...         print("DEL")
...         global X
...         X = self
...
py> C()
<__main__.C object at 0xb785c48c>
py> print(X)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'X' is not defined
py> X = 0
py> print(X)
0


I've tried it in both Python 2.7 and 3.5 and get the same NameError. I
suspect that you are not running the code you have shown us.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list