On Thu, May 31, 2018 at 11:23 PM, Peter O'Connor
<peter.ed.ocon...@gmail.com> wrote:
> On Thu, May 31, 2018 at 2:55 PM, Chris Angelico <ros...@gmail.com> wrote:
>>
>> [process(tx, y) for x in xs for tx in [transform(x)] for y in yz]
>>
>> ...
>>
>> I think Serhiy was trying to establish this form as a standard idiom,
>> with optimization in the interpreter to avoid constructing a list and
>> iterating over it (so it would be functionally identical to actual
>> assignment). I'd rather see that happen than the creation of a messy
>> 'given' syntax.
>
>
> Perhaps it wouldn't be crazy to have "with name=initial" be that idiom
> instead of "for name in [initial]".  As ..
>
>     [process(tx, y) for x in xs with tx=transform(x) for y in yz]
>
> .. seems to convey the intention more clearly.  More generally (outside of
> just comprehensions), "with name=expr:" could be used to temporarily bind
> "name" to "expr" inside the scope of the with-statement (and unbind it at
> the end).

Except that 'with' means context managers, not just assignment. Also,
it's not backward-compatible; if the "for var in [val]" syntax becomes
an accepted idiom, it'll be valid in all versions of Python back to,
what, 2.4? and just won't be optimized in older versions. Making a new
syntax misses out on that benefit, so it needs to be a really good
syntax, and 'with' isn't.

> And then I could have my precious initialized generators (which I believe
> cannot be nicely implemented with ":=" unless we initialize the variable
> outside of the scope of the comprehension, which introduces the problem of
> unintended side-effects).
>
>     smooth_signal = [average with average=0 for x in seq with
> average=(1-decay)*average + decay*x]

I want to read this as "average with average=0" blah blah, which
doesn't make a lot of sense. It'd be far FAR better to mess with
things so that external assignment works.

ChrisA
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to