[Python-Dev] Re: accumulator display syntax

Nick Coghlan ncoghlan at iinet.net.au
Wed Oct 22 09:02:52 EDT 2003

Alex Martelli strung bits together to say:

> I don't think we should encourage that sort of thing with the "implicit
> assignment" in accumulation.
> So, if it's an accumulation syntax we're going for, I'd much rather find
> ways to express whether we want [a] no assignment at all (as e.g for
> union_update), [b] plain assignment, [c] augmented assignment such
> as += or whatever.  Sorry, no good idea comes to my mind now, but
> I _do_ think we'd want all three possibilities...

I had a similar thought about 5 minutes after turning my computer off last 
night. The alternative I came up with was:

   y = (from result = 0.0 do result += x**2 for x in values if x > 0)

The two extra clauses (from & do) are pretty much unavoidable if we want to be 
able to express both the starting point, and the method of accumulation. And 
hopefully those clauses would be enough to disambiguate this from the new syntax 
for generator expressions.

The 'from' clause would allow a single plain assignment statement. It names the 
accumulation variable, and also gives it an initial value (if you don't want an 
initial value, an explicit assignment to None should suffice)

The 'do' clause would allow single plain or augmented assignment statements, as 
well as allowing any expression.

'from' is already a keyword (thanks to 'from ... import ...') and it might be 
possible to avoid making 'do' a keyword (in the same way that 'as' is not a 
keyword despite its use in 'from ... import ... as ...')

(And I'll add my vote to pointing out that generator expressions don't magically 
eliminate the use of the reduce function or accumulation loops any more than 
list comprehensions did. We still need the ability to express the starting value 
and the accumulation method).


P.S. I'm heading off to Canberra early tomorrow morning, so I won't be catching 
up on this discussion until the weekend.

Nick Coghlan           |              Brisbane, Australia
ICQ#: 68854767         |               ncoghlan at email.com
Mobile: 0409 573 268   |   http://www.talkinboutstuff.net
"Let go your prejudices,
               lest they limit your thoughts and actions."

More information about the Python-Dev mailing list