471,350 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

questions about functions inside a function

I want to create a list of function.

Here is my code:
In [9]: a = []

In [10]: for i in range(4):
....: b = lambda : i**2
....: a.append(b)
....:
....:

In [11]: for f in a:
....: f()
....:
....:
9
9
9
9

What I want is, the value of i should be bounded to the anonymous function.
And the output should like this:

for f in a:
print f()
0
1
4
9

How to achieve this?

Thanks a lot!
Jul 16 '07 #1
5 1165
"fd********@gmail.com" <fd********@gmail.comwrote:
What I want is, the value of i should be bounded to the anonymous
function. And the output should like this:

for f in a:
print f()
0
1
4
9

How to achieve this?
Use default arguments when defining your function: default arguments are
evaluated when the function is defined, bound arguments get the current
value of the variable at the point when it is referenced during the call.

Also, since you give your function a name anyway ('b') you might as well
use a named function as being clearer than using lambda:
>>a = []
for i in range(4):
def b(i=i):
return i**2
a.append(b)

>>for f in a:
f()
0
1
4
9
>>>
Jul 16 '07 #2
fd********@gmail.com a écrit :
I want to create a list of function.

Here is my code:
In [9]: a = []

In [10]: for i in range(4):
....: b = lambda : i**2
....: a.append(b)
....:
....:

In [11]: for f in a:
....: f()
....:
....:
9
9
9
9

What I want is, the value of i should be bounded to the anonymous
function. And the output should like this:

for f in a:
print f()
0
1
4
9

How to achieve this?
>>a = [lambda i=i:i**2 for i in range(4)]
for f in a:
.... print f()
....
0
1
4
9
>>>
HTH
Jul 16 '07 #3
On Jul 16, 4:49 am, "fdu.xia...@gmail.com" <fdu.xia...@gmail.com>
wrote:

(snipped)
>
What I want is, the value of i should be bounded to the anonymous function.
And the output should like this:

for f in a:
print f()
0
1
4
9

How to achieve this?

Thanks a lot!


The functools module, in Python 2.5 allows:
>>import functools
square = lambda i: i ** 2
anon = [functools.partial(square, i) for i in range(4)]
for f in anon:
.... print f()
....

--
Hope this helps,
Steven

Jul 16 '07 #4
fd********@gmail.com wrote:
What I want is, the value of i should be bounded to the anonymous
function. And the output should like this:
....
How to achieve this?
This doesn't answer your question (others have), but another (perhaps
clearer) way to do such things is something like

class MyFunc(object):
"""A function object."""
def __init__(self, val):
self.val = val

def __call__(self):
"""Return value squared"""
return self.val**2

a = []
for i in range(4):
a.append(MyFunc(i))

for f in a:
f()
Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
Jul 16 '07 #5
Jeremy Sanders a écrit :
fd********@gmail.com wrote:

>>What I want is, the value of i should be bounded to the anonymous
function. And the output should like this:

...
>>How to achieve this?


This doesn't answer your question (others have), but another (perhaps
clearer) way to do such things is something like

class MyFunc(object):
"""A function object."""
def __init__(self, val):
self.val = val

def __call__(self):
"""Return value squared"""
return self.val**2

a = []
for i in range(4):
a.append(MyFunc(i))

for f in a:
f()
I wouldn't say it's "clearer" - at least in this concrete use case. And
it takes more than just defining the __call__ method to have a fully
function-like object (hint: try using a MyFunc instance as a method).
Jul 16 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Pelle Beckman | last post: by
4 posts views Thread by Rhino | last post: by
15 posts views Thread by copx | last post: by
9 posts views Thread by questions? | last post: by
39 posts views Thread by Digital Puer | last post: by
13 posts views Thread by John Dann | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.