M
Max Rybinsky
Hello!
Please take a look at the example.
8), (9, 9)]
Now i want to get a list of functions x*y/n, for each (x, y) in a:
It looks consistent!
[<function <lambda> at 0x010F3DF0>, <function <lambda> at 0x010F7CF0>,
<function <lambda> at 0x010F7730>, <function <lambda> at 0x010FD270>,
<function <lambda> at 0x010FD0B0>, <function <lambda> at 0x010FD5B0>,
<function <lambda> at 0x010FD570>, <function <lambda> at 0x010FD630>,
81
But they aren't!
.... print func(1)
....
81
81
81
81
81
81
81
81
81
81
It seems, all functions have x and y set to 9.
What's wrong with it? Is it a bug?
.... return lambda n: x * y / n
....
.... and it does work!
But why not to save several lines of code?
Thanks in advance.
Please take a look at the example.
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8,a = [(x, y) for x, y in map(None, range(10), range(10))] # Just a list of tuples
a
8), (9, 9)]
Now i want to get a list of functions x*y/n, for each (x, y) in a:
funcs = [lambda n: x * y / n for x, y in a]
It looks consistent!
[<function <lambda> at 0x010F3DF0>, <function <lambda> at 0x010F7CF0>,
<function <lambda> at 0x010F7730>, <function <lambda> at 0x010FD270>,
<function <lambda> at 0x010FD0B0>, <function <lambda> at 0x010FD5B0>,
<function <lambda> at 0x010FD570>, <function <lambda> at 0x010FD630>,
[/QUOTE][QUOTE= said:funcs[0](1)
81
But they aren't!
.... print func(1)
....
81
81
81
81
81
81
81
81
81
81
It seems, all functions have x and y set to 9.
What's wrong with it? Is it a bug?
.... return lambda n: x * y / n
....
funcs = [buldFunc(x, y) for x, y in a]
.... and it does work!
But why not to save several lines of code?
Thanks in advance.