[Python-ideas] A real life example of "given"
Peter O'Connor
peter.ed.oconnor at gmail.com
Thu May 31 09:23:13 EDT 2018
On Thu, May 31, 2018 at 2:55 PM, Chris Angelico <rosuav at 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).
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]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180531/a8fa699f/attachment.html>
More information about the Python-ideas
mailing list