E
Ed Schofield
Hi all,
I find this strange:
.... flist.append(f)
....
What I expect is:
Is this a bug in Python? It happens on my builds of
Python 2.3 and 2.2.3.
Replacing the lambda function by a named function, as in
flist = []
for i in range(3):
def f(x):
return x + i
flist.append(f)
[f(1) for f in flist]
gives the same result.
I have a workaround (of sorts) adapted from the Python tutorial:
.... return lambda x: x + n
....
but I'd prefer the flexibility of the first approach. Any ideas?
Any explanations for why Python does this? Any justifications for why
Python _should_ do this? Who believes this is a bug?
Kind regards,
Ed Schofield
I find this strange:
.... f = lambda x: x+iflist = []
for i in range(3):
.... flist.append(f)
....
[f(1) for f in flist] [3, 3, 3]
What I expect is:
[f(1) for f in flist] [1,2,3]
Is this a bug in Python? It happens on my builds of
Python 2.3 and 2.2.3.
Replacing the lambda function by a named function, as in
flist = []
for i in range(3):
def f(x):
return x + i
flist.append(f)
[f(1) for f in flist]
gives the same result.
I have a workaround (of sorts) adapted from the Python tutorial:
.... return lambda x: x + n
....
flist = [make_incrementor(i) for i in range(3)]
[f(1) for f in flist] [1, 2, 3]
but I'd prefer the flexibility of the first approach. Any ideas?
Any explanations for why Python does this? Any justifications for why
Python _should_ do this? Who believes this is a bug?
Kind regards,
Ed Schofield