Callable generators (PEP 288: Generator Attributes, again)

Aahz aahz at pythoncraft.com
Wed Nov 19 13:51:00 EST 2003


In article <vrmr18fktbfh69 at corp.supernews.com>,
Francis Avila <francisgavila at yahoo.com> wrote:
>Aahz wrote in message ...
>>In article <55688f89.0311180211.7ab1bc30 at posting.google.com>,
>>Francis Avila <francisgavila at yahoo.com> wrote:
>>>
>>>A little annoyed one day that I couldn't use the statefulness of
>>>generators as "resumable functions", [...]
>>
>><raised eyebrow>  But generators *are* resumable functions; they just
>>don't permit injection of new values into their state.
>
>I see then that I don't need to convince you. :) But it is because you
>can't inject new values into their state that they are not resumable
>functions.  They're pure state, not functions-with-persisting-state.
>If they were resumable functions, we could call them like functions
>and be returned values based upon passed parameters, except that the
>algorithm used would depend upon the generator's internal state.

Enh.  Depends on how one looks at it.  They *are* functions with
persisting state; the state stays around until the generator's iterator
exits.  Sounds to me like you're trying to conflate two different
mechanisms for managing function state.

>>The problem with injecting values is that there's no way to pick them
>>up; it's a "push" solution rather than a "pull" solution.
>
>You mean "pull" rather than "push"?

No, I do mean push.

>Well, I just suggested a way to pick them up which is no different
>than how a function picks up parameters--they're pushed in rather than
>pulled in, by overwriting the local namespace before advancing the
>generator's state.  Would you care to comment on my suggestion?

The problem is that a function declares what parameters are to be pushed
in, so that from the standpoint of the function, it really is a pull
solution.  When you instantiate a generator iterator, the bytecodes in
the function do *not* get executed until the first call to the iterator's
next() method.  What's supposed to happen when someone pushes additional
values in before the first call to next()?  How is the function supposed
to declare what values may be pushed in?  You're simply not going to
persuade Guido that arbitrary values should be pushed.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.




More information about the Python-list mailing list