Generator inside a class prevent __del__ ??

Mark Day mday at apple.com
Tue Apr 20 21:00:31 EDT 2004


In article <4085BA96.651D2E4A at free.fr>, Emmanuel <eastier at free.fr>
wrote:

> >>> class toto:
>             def __init__(self):
>                 print "init"
>                 self.Coroutine = self.Gen()
>             def __del__(self):
>                 print "del"
>             def Gen(self):
>                 yield 1
> 
> >>> a = toto()
> init
> >>> c = []
>                 <--- Nothing there !!!

First of all, "a" is still referencing your toto object.  I think you
meant "a = []" here.  But even if you did "a = []", the destructor
still isn't called.  There must still be a reference to the object.  My
guess is that the generator (directly or indirectly) is referencing the
object, creating a self referential loop.

Consider the following modification that merely references a function,
and does not create a generator:

>>> class tata:
...     def __init__(self):
...             print "init"
...             self.Coroutine = self.Gen
...     def __del__(self):
...             print "del"
...     def Gen(self):
...             pass
... 
>>> a=tata()
init
>>> a=[]
>>>

Here's how to break that loop:

>>> b=tata()
init
>>> b.Coroutine=None
>>> b=[]
del
>>>

-Mark



More information about the Python-list mailing list