[Python-Dev] Re: accumulator display syntax

Andrew Bennetts andrew-pythondev at puzzling.org
Mon Oct 20 10:30:56 EDT 2003

Greg Ewing wrote:
> how about
>   sum of x*x for x in xvalues
>   average of g for g in grades
>   maximum of f(x, y) for x in xrange for y in yrange
>   top(10) of humour(joke) for joke in comedy

I've thought about this, and I don't think I like it.  "of" just seems like
a new and confusingly different way to spell a function call.  E.g., if I
read this
    max([f(x,y) for x in xrange for y in yrange])
out-loud, I'd say:
    "the maximum of f of x and y for x in xrange, and y in yrange"

So perhaps that third example should be spelt:
    maximum of f of x, y for x in xrange for y in yrange

This particularly struck me when I read Alex's comment:

> for x in sorted_copy of mylist:
>     ...
> now doesn't THAT read just wonderfully, too...?-)

Actually, that strikes me as an odd way of spelling:

for x in sorted_copy(mylist):

I think the lazy iteration syntax approach was probably a better idea.  I
don't like the proposed use of "yield" to signify it, though -- "yield" is a
flow control statement, so the examples using it in this thread look odd to
me.  Perhaps it would be best to simply use the keyword "lazy" -- after all,
that's the key distinguishing feature.  I think my preferred syntax would

    sum([lazy x*x for x in sequence])

But use of parens instead of brackets, and/or a colon to make the keyword
stand out (and look reminisicent to a lambda! which *is* a related concept,
in a way -- it also defers evaluation), e.g.:

    sum((lazy: x*x for x in sequence))

Would be fine with me as well.


More information about the Python-Dev mailing list