Callable generators (PEP 288: Generator Attributes, again)

Francis Avila francisgavila at yahoo.com
Wed Nov 19 19:49:35 EST 2003


Michele Simionato wrote in message
<95aa1afa.0311191028.77aa02a3 at posting.google.com>...
>"Francis Avila" <francisgavila at yahoo.com> wrote in message
news:<vrmpthqfk8bc8b at corp.supernews.com>...
>> I'm suggesting the PEP's functionality, not its syntax and semantics.  My
>> contention is that the PEP regards generators as too class-like, when
they
>> are more naturally considered as function-like.
>>
>> For example, your iterator class/instance would look like this:
>>
>> def iterator(x=1)(x):
>>     yield x
>>     yield x
>>
>> print iterator.next() # -> 1
>> print iterator(5)  # -> 5
>>
>> The local name "x" is updated (according to the second parameter list in
the
>> function definition) right after the yield of the previous call when
>> iterator is called, behaving like a state-persistent callable function.
If
>> it's just "nexted", it behaves like a plain old iterator.
>
>I see what you mean, now. Still, the notation is confusing, since I
>do think an iterator as something which it obtained by "instantiating"
>a generator. On the other hand, your iterator(5) would not be a newly
>"instantiated" iterator, but the same iterator with a different
>parameter x. So, you would need a different syntax, as for instance
>iterator[5].

I'm sorry, it was supposed to be 'def MyIterator', then 'iterator =
MyIterator()' as Bengt points out.

What I mean is this:

def generator_factory(<generator_factory's parameter list>) (<returned
generator's param list>):
    # blah blah
    yield None

I'm not married to this syntax, but I do think its important that it be
possible to give the function and the generator different param lists, and
that the generator be iterable *or* callable.  Also, I *think* (but am not
absolutely sure--must look more deeply at code and function objects....)
that Python *must* know about *both* parameter lists at compile time, so
that LOAD_FASTs will work properly, and so that the generator can have
references to the names in co_varnames to update those names in *its* param
list (to update the locals).
--
Francis Avila






More information about the Python-list mailing list