[Python-ideas] Proposal: A Reduce-Map Comprehension and a "last" builtin
peter.ed.oconnor at gmail.com
Wed May 30 12:05:33 EDT 2018
On Thu, May 24, 2018 at 2:49 PM, Steven D'Aprano <steve at pearwood.info>
> On Thu, May 24, 2018 at 02:06:03PM +0200, Peter O'Connor wrote:
> > We could use given for both the in-loop variable update and the variable
> > initialization:
> > smooth_signal = [average given average=(1-decay)*average + decay*x
> > x in signal] given average=0.
> So in your example, the OUTER "given" creates a local variable in the
> current scope, average=0, but the INNER "given" inside the comprehension
> exists inside a separate, sub-local comprehension scope, where you will
> get an UnboundLocalError when it tries to evaluate (1-decay)*average the
> first time.
You're right, having re-thought it, it seems that the correct way to write
it would be to define both of them in the scope of the comprehension:
smooth_signal = [average given average=(1-decay)*average + decay*x for x
in signal given average=0.]
This makes sense and follows a simple rule: "B given A" just causes A to be
executed before B - that holds true whether B is a variable or a loop
declaration like "for x in x_gen".
a_gen = (g(a) given a=f(a, x) for x in x_gen given a=0)
would be a compact form of:
for x in x_gen:
a = f(a, x)
a_gen = a_gen_func()
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas