[Tutor] Shadow error

alan.gauld@bt.com alan.gauld@bt.com
Fri, 27 Jul 2001 10:54:46 +0100


> def dig(n=2):
> 	return lambda x:round(x,n)
> print map(dig(2),l)
> 
> Here's what I got:
> 
> <Untitled Script 2>:5: SyntaxWarning: local name 'n' in 'dig' shadows 
> use of 'n' as global in nested scope 'lambda'

So you need to do:

def dig(n=2):
 	return lambda x,m=n:round(x,m)
     
Or using the new nested scopes do:

from future import nested_scopes  # or something like that...
def dig(n=2):
   def f(x): return round(x,n)
   return f

> but I have generated the above error before and would like to know
> what I am doing wrong.

You used n within the lambda but n is only visible inside dig()
so when you pass the function to the map it can't see n.

I get caught by this regularly... :-)

Alan G