On Thu, May 24, 2018 at 2:49 PM, Steven D'Aprano wrote:
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 for
> 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".

So

a_gen = (g(a) given a=f(a, x) for x in x_gen given a=0)

would be a compact form of:

def a_gen_func(x_gen):
a=0
for x in x_gen:
a = f(a, x)
yield g(a)

a_gen = a_gen_func()