469,357 Members | 1,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,357 developers. It's quick & easy.

Not understanding lamdas and scoping

I am trying to use lamdba to generate some functions, and it is not working
the way I'd expect. The code is below, followed by the results I'm
getting. More comments below that.

patterns = (
('[sxz]$', '$','es'),
('[^aeioudgkprt]h$', '$', 'es'),
('[^aeiou]y$', 'y$', 'ies'),
('$', '$', 's'),
)

def z(*a):
print a

def make_pattern(pattern, search, replace):
def compare(word):
z(pattern, search, replace)
return compare

rules = [make_pattern(pattern, search, replace)
for (pattern, search, replace) in patterns]

print 'make pattern'
for rule in rules:
rule('noun')

rules = [lambda word: z(pattern, search, replace)
for (pattern, search, replace) in patterns]

print 'lambda/list comprehension'
for rule in rules:
rule('noun')

rules = []
for pattern, search, replace in patterns:
rules.append(lambda word: z(pattern, search, replace))

print 'lamda/for loop'
for rule in rules:
rule('noun')

Ouptut:

make pattern
('[sxz]$', '$', 'es')
('[^aeioudgkprt]h$', '$', 'es')
('[^aeiou]y$', 'y$', 'ies')
('$', '$', 's')
lambda/list comprehension
('$', '$', 's')
('$', '$', 's')
('$', '$', 's')
('$', '$', 's')
lamda/for loop
('$', '$', 's')
('$', '$', 's')
('$', '$', 's')
('$', '$', 's')

Of course, in my real code, I'm not calling z(), but a couple of RE
search/replace functions (yes, I'm working on the Dive Into Python
pluralizer).

The first result is obviously the correct one. But the next two don't make
sense. Why do all the functions returned by lamda have the last input
values?

What am I missing?

OK, I figured out if I did this:

def make_pattern(pattern, search, replace):
return lambda word: z(pattern, search, replace)

it would give correct results. So, is there some scoping issue with lambda
that I'm not seeing?

Thanks!

j

Mar 26 '08 #1
2 1126
On Mar 26, 5:02 pm, Joshua Kugler <jkug...@bigfoot.comwrote:
I am trying to use lamdba to generate some functions, and it is not working
the way I'd expect. The code is below, followed by the results I'm
getting. More comments below that.

(...)

So, is there some scoping issue with lambda
that I'm not seeing?
Yes; it's not related to lambda though but to closures (whether
defined as lambdas or regular functions). See for example
http://groups.google.com/group/comp....d1bba8ad56baf4

There should be an entry for this at http://www.python.org/doc/faq/programming/,
that's really an FAQ.

George
Mar 26 '08 #2
George Sakkis wrote:
On Mar 26, 5:02 pm, Joshua Kugler <jkug...@bigfoot.comwrote:
>I am trying to use lamdba to generate some functions, and it is not
working
the way I'd expect. The code is below, followed by the results I'm
getting. More comments below that.

(...)

So, is there some scoping issue with lambda
that I'm not seeing?

Yes; it's not related to lambda though but to closures (whether
defined as lambdas or regular functions). See for example
http://groups.google.com/group/comp....d1bba8ad56baf4
>
There should be an entry for this at
http://www.python.org/doc/faq/programming/, that's really an FAQ.
George -

Thanks for the quick and clear answer...that makes perfect sense. To
bad...I'd like to just use a list comprehension. :)

j

Mar 26 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Ian McMeans | last post: by
2 posts views Thread by David Stockwell | last post: by
14 posts views Thread by bruce stockwell | last post: by
4 posts views Thread by Joel Gordon | last post: by
9 posts views Thread by NevilleDNZ | last post: by
17 posts views Thread by Chad | last post: by
3 posts views Thread by SPECTACULAR | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.