[Tutor] Thoughts on little lambda

Doug.Shawhan@gecits.ge.com Doug.Shawhan@gecits.ge.com
Fri, 8 Mar 2002 16:17:12 -0500


Errr...

I tried this particular excersise and had a problem...

------------------------------------------------------
Python 2.1.1 (#20, Jul 20 2001, 01:19:29) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> def addn(n):
	return lambda x:x+n
SyntaxError: local name 'n' in 'addn' shadows use of 'n' as global in nested
scope 'lambda' (<pyshell#1>, line 1)
------------------------------------------------------

Am I missing something?


Kirby said:

You can do this in Python too (but not in C):

# Python
def addn (n):
   return lambda x: x + n

Note we've got the lexical scoping going, so n binds
to the passed (n).

   >>> add1 = addn(1)
   >>> add1(2)
   3
   >>> add2 = addn(2)
   >>> add2(2)
   4

So far so good.