__del__ not called?

bruno at modulix onurb at xiludom.gro
Mon Mar 13 13:32:22 CET 2006

Gregor Horvath wrote:
> Felipe Almeida Lessa schrieb:
>>>>>del B
>>>>># We'll to tell him to collect the garbage here, but
>> ... # usually it should not be necessary.
> Thanks. If I do
> del B
> then the __del__ of A gets called.
> That surprises me.

Why ?

> I thought that B gets del'd by python when it goes
> out of scope?

It does. What you have to understand here is that in your script, B
being in the global (read : module) scope, it doesnt goes out of scope
before the script's execution's done. By that time, it's too late to do
anything with stdin/stdout/stderr.

Just add this to the script:

def foo():
  b = B()
  print "in foo"


> Do I manually have to del all class objects, so that their class
> attributes get gc'd ????

Absolutely not. The only times I use del is when I want to cleanup a
namespace from temp vars no longer used (usually a module's namespace
with some hairy tricks at load time).

bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"

More information about the Python-list mailing list