By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,550 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

Not understanding lamdas and scoping

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.