[Python-Dev] accumulator display syntax
Guido van Rossum
guido at python.org
Tue Oct 21 15:36:40 EDT 2003
> Skip is right about returning to the basics. Before considering
> some of the wild syntaxes that have been submitted, I suggest
> re-examining the very first proposal with brackets and yield.
> At one time, I got a lot of feedback on this from comp.lang.python.
> Just about everyone found the brackets to be helpful and not misleading,
> the immediate presence of "yield" was more than enough to signal that
> an iterator was being returned instead of a list:
> g = [yield (len(line),line) for line in file if len(line)>5]
> This syntax is instantly learnable from existing knowledge about
> list comprehensions and generators. The advantage of a near zero
> learning curve should not be easily dismissed.
> Also, this syntax makes is trivially easy to convert an existing
> list comprehension into an iterator comprehension if needed to
> help the application scale-up or to improve performance.
I expect that most iterator comprehensions (we need a better term!)
are not stored in a variable but passed as an argument to something
that takes an iterable, e.g.
sum(len(line) for line in file if line.strip())
I find that in such cases, the 'yield' distracts from what is going on
by focusing attention on the generator (which is really just an
We can quibble about whether double parentheses are needed, but this
syntax is just so much clearer than the version with square brackets
and yield, that there is no contest IMO.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev