Generator inside a class prevent __del__ ??

Emmanuel eastier at free.fr
Thu Apr 22 05:26:11 EDT 2004



Terry Reedy a écrit :

> "Emmanuel" <eastier at free.fr> wrote in message
> news:40866ECD.A45758B9 at free.fr...
> >
> >
> > Terry Reedy a écrit :
> > > > >>> 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.
>
> To amplify: the usual idiom for an instance-associated generator is to name
> the generator function (method) __iter__ (with one param,  self) and to
> create and get a reference to the generator via iter() or let the for loop
> mechanism do so for you.
>
> c = C(*args)
> cgen =iter(c)
>
> Then there is no reference loop.  And you can pass around the cgen object
> just like any other.  If you only need the instance after initialization to
> get the generator and you only need one generator for the instance, then
> combine the two lines into
>
> cgen = iter(C(*args))
>
> and the *only* reference to the instance is the one in the generator, which
> will disappear at the end of a for loop or with an explicit 'del cgen'.

But I obviously need other references to my object in my code, my object isn't
modified by the generator only.
I want to resume my generator from time to time during the execution of my app,
and to modify the members of the objects somewhere else ( interaction between
my objects ).
Doing this result in my nicer programmation style than without generators.


>
>
> There is also the question whether you actually *need* to get rid of the
> object while the program is still running instead of just letting the
> program finish and clean up.
>

I have a _lot_ of objects created whenever they want, and I don't know where
they will finish their job.
Additionnaly, I'm not developping only on PC, but also on platforms where there
is not so much memory avalaible.

By the way, it seems I still have a lot to understand on this subject.
Do you know any link, example, or whatever, that I could have a look at ?

Thank you very much for your answers,

Emmanuel





More information about the Python-list mailing list