Destruction of generator objects

Stefan Bellon sbellon at sbellon.de
Sat Aug 11 08:50:33 EDT 2007


On Sat, 11 Aug, Kay Schluehr wrote:

> But why shall the destructor be called? Your example does not indicate
> that a ListGenerator object is somewhere destroyed neither explicitely
> using del nor implicitely by destroying the scope it is living in.

After having constructed the list itself, the generator is exhausted
and not iterated or referenced anymore, so the generator should be
destroyed, shouldn't it?

Ok, let's make the example easier and take out the external iterator
resource and just concentrate on the Python part:

class ListGenerator(object):
    def __init__(self):
        print "gen init"
        self.gen = self._value()

    def __del__(self):
        print "gen del"

    def _value(self):
        print "gen value"
        for i in xrange(4):
            yield i

    def __iter__(self):
        print "gen iter"
        return self

    def next(self):
        print "gen next"
        return self.gen.next()

Now, doing the following:

>>> a = ListGenerator()
gen init
>>> a
<__main__.ListGenerator object at 0x4020c3ec>
>>> for i in a: print i
... 
gen iter
gen next
gen value
0
gen next
1
gen next
2
gen next
3
gen next
>>> del a
>>>

So why is the destructor not called when the generator is even
explicitly 'del'ed? Does somebody else still hold a reference on it?
But then, even when terminating the interpreter, __del__ is not called.
When taking out the yield statement, __del__ is called again. It looks
to me that as soon as a generator function is involved in the class,
the __del__ is not called anymore.

-- 
Stefan Bellon



More information about the Python-list mailing list