![](https://secure.gravatar.com/avatar/d67ab5d94c2fed8ab6b727b62dc1b213.jpg?s=120&d=mm&r=g)
On Thu, Mar 1, 2018 at 2:10 AM, Ethan Furman <ethan@stoneleaf.us> wrote:
dis.dis may be great, but so is running the function so everyone can see the output. ;)
Oh, sorry.
f() 1
If I understood your explanation, `print(a)` produces `1` ? That seems wrong -- the point of statement-local name bindings is twofold:
- give a name to a value - evaluate to that value
Which is why your first example works:
stuff = [[(f(x) as y), y] for x in range(5)]
(f(x) as y), y
evaluates as f(x), and also assigns that result to y, so in
a = (2 as a)
there is a temporary variable 'a', which gets assigned 2, and the SLNB is evaluated as 2, which should then get assigned back to the local variable 'a'. In other words, the final print from `f()` above should be 2, not 1. (Slightly different names would help avoid confusion when referencing different locations of the PEP.)
Except that assignment is evaluated RHS before LHS as part of a single statement. When Python goes to look up the name "a" to store it (as the final step of the assignment), the SLNB is still active (it's still the same statement - note that this is NOT expression-local), so it uses the temporary. Honestly, though, it's like writing "a = a++" in C, and then being confused by the result. Why are you using the same name in two assignments? Normal code shouldn't do this. :) ChrisA