Generator inside a class prevent __del__ ??

Terry Reedy tjreedy at udel.edu
Tue Apr 20 20:29:07 EDT 2004


"Emmanuel" <eastier at free.fr> wrote in message
news:4085BA96.651D2E4A at free.fr...
> I run across this problem, and couldn't find any solution (python 2.2.2)
...
> Here, everything is normal...
> But creating a generator :

You both defined generator function and called it to create generator
iterator.

>
> Code :
> ===========
>
> >>> class toto:
>             def __init__(self):
>                 print "init"
>                 self.Coroutine = self.Gen()

This creates a reference loop.  Delete this (and correct typo below) and
'problem' will disappear.

>             def __del__(self):
>                 print "del"
>             def Gen(self):

If you do not really use self in the resulting iterator, define this
outside of the class without self as a parameter, and problem will
disappear.

>                 yield 1
>
> >>> a = toto()

did you mean 'c = toto()'?

> init
> >>> c = []
>                 <--- Nothing there !!!
> ==============
>
> I can't understand why the destructor is not called when a generator is
> created, and what I should do to have a "correct" behavior.

Either do not create reference loop or break it with del c.Coroutine.

Terry J. Reedy








More information about the Python-list mailing list