Destruction of generator objects
Kay Schluehr
kay.schluehr at gmx.net
Sat Aug 11 07:19:33 EDT 2007
On Aug 11, 12:16 pm, Stefan Bellon <sbel... at sbellon.de> wrote:
> On Sat, 11 Aug, Kay Schluehr wrote:
> > On Aug 9, 1:14 am, Stefan Bellon <sbel... at sbellon.de> wrote:
> > > Sorry, I forgot to mention that I am forced to using Python 2.4.
> > It doesn't matter. You can use try...finally as well in Python 2.4.
> > It's just not possible to use except and finally clauses in one
> > statement such as:
>
> [snip]
>
> The problem is yield inside try-finally which is not possible in 2.4.
>
> --
> Stefan Bellon
Oh, yeah... i overlooked this.
Honestly, I'd recommend wrapping the generator into a function object,
create the resource on construction ( or pass it ) and destroy it
implementing __del__.
def gen_value(self):
while True:
yield self.iter.next()
class GeneratorObj(object):
def __init__(self, obj, gen):
self.iter = make_iter(obj)
self.gen = gen(self)
def __del__(self):
destroy(self.iter)
def next(self):
return self.gen.next()
More information about the Python-list
mailing list