[Python-Dev] Re: accumulator display syntax

Alex Martelli aleaxit at yahoo.com
Tue Oct 21 12:41:45 EDT 2003

On Tuesday 21 October 2003 03:41 pm, Nick Coghlan wrote:
> What about:
>    result + x**2 from result = 0.0 for x in S
> Essentially short for:
>    result = 0.0
>    for x in S:
>      result = result + x**2

Not bad, but I'm not sure I like the strict limitation to "A = A + f(x)" forms
(possibly with some other operator in lieu of + etc, of course).  Say I
want to make a sets.Set out of the iterator, for example:

result.union([ x**2 ]) from result = sets.Set() for x in theiter

now that's deucedly _inefficient_, consarn it!, because it maps to a
loop of:
    result = result.union([ x** ])

so I may be tempted to try, instead:

real_result = sets.Set()
real_result.union_update([ x**2 ]) from fake_result = None for x in theiter

and hoping the N silly rebindings of fake_result to None cost me less
than not having to materialize a list from theiter would cost if I did
    real_result = sets.Set([ x**2 for x in theiter ])

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...


More information about the Python-Dev mailing list