Something is rotten in Denmark...
Terry Reedy
tjreedy at udel.edu
Tue May 31 13:11:43 EDT 2011
On 5/31/2011 2:48 AM, harrismh777 wrote:
>>>> fs=[]
Irrelevant here since you immediately rebind 'fs'.
>>>> fs = [(lambda n: i + n) for i in range(10)]
>>>> [fs[i](1) for i in range(10)]
Same as [f(1) for f in fs]
> [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] <=== not good
>
> ( that was a big surprise! . . . )
You have been hypnotizeed by lambda. (lambda n: i+n) is a *constant
expression*, so you get 10 'equal' functions. To see this better
fs = [(lambda n: i + n) for i in range(10)]
from dis import dis
for f in fs: dis(f)
1 0 LOAD_DEREF 0 (i)
3 LOAD_FAST 0 (n)
6 BINARY_ADD
7 RETURN_VALUE
1 0 LOAD_DEREF 0 (i)
3 LOAD_FAST 0 (n)
6 BINARY_ADD
7 RETURN_VALUE
...
All have the same bytecode and all retrieve the same last value of i in
the nonlocal listcomp scope when you call them *after* the listcomp
scope has otherwise disappeared.
Your code is equivalent to
fs = []
for i in range(10):
fs.append(lambda n: i + n)
print([f(1) for f in fs])
which is equivalent (except for naming the functions) to
fs = []
for i in range(10):
def f(n): return i + n
fs.append(f)
print([f(1) for f in fs])
Does [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] still surprise?
Because the def is a constant expression, we can move it out of the
loop, and get the equivalent (except for identity)
def f(n): return i + n
fs = []
for i in range(10):
fs.append(f)
print([f(1) for f in fs])
This in turn is equivalent to
def f(n): return i + n
fs = []
for _ in range(10):
fs.append(f)
i=9
print([f(1) for f in fs])
which in turn is equivalent in output to
def f(n): return i + n
i = 9
print([f(1) for _ in range(10)])
Note that:
def f(n): return i+n # or f = lambda n: i+n
fs = [f for i in range(10)]
print([f(1) for f in fs])
works in 2.7, with the same output, but not in 3.2 because in 3.x, i is
local to the list comp and the later call raises unbound global error.
> ( let's try it another way . . . )
All these other ways create 10 *different* (unequal) functions that are
different because they have captured 10 different values of i when
defined instead of deferring lookup of i to when they are called.
def g(i): return (lambda n: i + n)
fs = [g(i) for i in range(10)]
print([f.__closure__[0].cell_contents for f in fs])
fs = [(lambda n, i=i: i + n) for i in range(10)]
print([f.__defaults__[0] for f in fs])
# CPython 3.2 dependent code !!!
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
--
Terry Jan Reedy
More information about the Python-list
mailing list