What I want to do is build an array of lambda functions, like so:
a = [lambda: i for i in range(10)]
(This is just a demonstrative dummy array. I don't need better ways to
achieve the above functionality.)
print [f() for f in a]
results in: [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
rather than the hoped for: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Clearly, lambda is returning the object i, which is left at the last
value of range(10). The following is my solution.
t = lambda i: lambda: i
a = [t(i) for i in range(10)]
or the somewhat more terse:
a = [(lambda i: lambda: i)(i) for i in range(10)]
This gives the behavior which, intuitively, I expected from the
original syntax. So my questions are:
1) Does this make sense as what should be done here? That is, would
this be the behavior you'd want more often than not? As I said,
intuitively, I would think the lambda would treat the iterator
variable as a constant in this context.
2) Is there a better or preferred method than the one I've found?
Thanks,
Chris 5 1624
Chris Johnson <ef******@gmail.comwrites:
a = [lambda: i for i in range(10)]
print [f() for f in a]
results in: [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
rather than the hoped for: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
The usual idiom is
a = [lambda i=i: i for i in range(10)]
That way i is not a free variable in the lambda.
Chris Johnson schrieb:
What I want to do is build an array of lambda functions, like so:
a = [lambda: i for i in range(10)]
(This is just a demonstrative dummy array. I don't need better ways to
achieve the above functionality.)
print [f() for f in a]
results in: [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
rather than the hoped for: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Clearly, lambda is returning the object i, which is left at the last
value of range(10). The following is my solution.
t = lambda i: lambda: i
a = [t(i) for i in range(10)]
or the somewhat more terse:
a = [(lambda i: lambda: i)(i) for i in range(10)]
This gives the behavior which, intuitively, I expected from the
original syntax. So my questions are:
1) Does this make sense as what should be done here? That is, would
this be the behavior you'd want more often than not? As I said,
intuitively, I would think the lambda would treat the iterator
variable as a constant in this context.
2) Is there a better or preferred method than the one I've found?
The problem you encountered relates to the fact that the lambdas close
around the names known when they were created - not the values bound to
them.
To overcome that, bind the value you pass to a new name, like this:
a = [lambda i=i: i for i in range(10)]
Diez
Chris Johnson <ef******@gmail.comwrote:
2) Is there a better or preferred method than the one I've found?
Use function default arguments to keep the current value of i at the point
where you define the function.
a = [(lambda n=i: n) for i in range(10)]
Chris Johnson <ef******@gmail.comwrites:
What I want to do is build an array of lambda functions, like so:
a = [lambda: i for i in range(10)]
Use a factory function for creating the lambdas. The explicit
function call will force a new variable binding to be created each
time, and the lambda will refer to that binding rather than to the
loop variable binding, which is reused for all loop iterations. For
example:
def makefn(i):
return lambda: i
>>a = [makefn(i) for i in xrange(10)] [f() for f in a]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
The alternative is to explicitly import the value into the lambda's
parameter list, as explained by others.
On Sep 6, 3:44 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
Chris Johnson <effig...@gmail.comwrites:
a = [lambda: i for i in range(10)]
print [f() for f in a]
results in: [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
rather than the hoped for: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
The usual idiom is
a = [lambda i=i: i for i in range(10)]
That way i is not a free variable in the lambda.
Thanks. I figured there had to be a better way than what I was doing. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Christian Tismer |
last post by:
Hi Pythonistas,
just as a small comment on the side-effects of the
rather new concept of local functions with access to
their scope:
This concept can be used to avoid having extra
classes...
|
by: Nick Coghlan |
last post by:
GvR has commented that he want to get rid of the lambda keyword for Python 3.0.
Getting rid of lambda seems like a worthy goal, but I'd prefer to see it dropped
in favour of a different syntax,...
|
by: Kay Schluehr |
last post by:
Hi all,
thanks for Your attention !
I think my proposal was more in mind of Rons modified exec than
Pythons lambda.
When George proposed his unpacking behavoir for list-comps as a pack of...
|
by: Scott David Daniels |
last post by:
Denis Kasak wrote:
....
Yes, you are missing something: binding time. your anonymous function
returns the value of "i" in the enclosing scope _at_the_time_of_
_the function's_execution_. If...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
| |