[Python-Dev] Re: anonymous blocks
Guido van Rossum
gvanrossum at gmail.com
Thu Apr 28 02:43:19 CEST 2005
> It seems like what you are proposing is a limited form of
Well, I though that's already what generators were -- IMO there isn't
much news there. We're providing a more convenient way to pass a value
back, but that's always been possible (see Fredrik's examples).
> Allowing 'continue' to have an optional value is elegant syntax.
> I'm a little bit concerned about what happens if the iterator does
> not expect a value. If I understand the PEP, it is silently
> ignored. That seems like it could hide bugs. OTOH, it doesn't seem
> any worse then a caller not expecting a return value.
> It's interesting that there is such similarity between 'for' and
> 'block'. Why is it that block does not call iter() on EXPR1? I
> guess that fact that 'break' and 'return' work differently is a more
> significant difference.
Well, perhaps block *should* call iter()? I'd like to hear votes about
this. In most cases that would make a block-statement entirely
equivalent to a for-loop, the exception being only when there's an
exception or when breaking out of an iterator with resource
I initially decided it should not call iter() so as to emphasize that
this isn't supposed to be used for looping over sequences -- EXPR1 is
really expected to be a resource management generator (or iterator).
> After thinking about this more, I wonder if iterators meant for
> 'for' loops and iterators meant for 'block' statements are really
> very different things. It seems like a block-iterator really needs
> to handle yield-expressions.
But who knows, they might be useful for for-loops as well. After all,
passing values back to the generator has been on some people's wish
list for a long time.
> I wonder if generators that contain a yield-expression should
> properly be called coroutines. Practically, I suspect it would just
> cause confusion.
I have to admit that I haven't looked carefully for use cases for
this! I just looked at a few Ruby examples and realized that it would
be a fairly simple extension of generators.
You can call such generators coroutines, but they are still generators.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev