[Tutor] Shadow error

Kalle Svensson kalle@gnupung.net
Fri, 27 Jul 2001 01:09:06 +0200


Sez Danny Yoo:
[about nested scopes and stuff]
> We're using stuff from the future.  *grin* If you put that line in the top
> of your file, the warning should disappear.  If you have questions, please
> feel free to ask them.

I'd like to ask a question, anyway.  Why is Cristopher's code working?  When
I try it with Python 2.1 I get

[ kalle aragorn ~ 00:59:52 ]$ python2.1 scopetest.py 
scopetest.py:5: SyntaxWarning: local name 'n' in 'dig' shadows use of 'n' as
global in nested scope 'lambda'
  def dig(n=2):
Traceback (most recent call last):
  File "scopetest.py", line 8, in ?
    print map(dig(2),l)
  File "scopetest.py", line 6, in <lambda>
    return lambda x:round(x,n)
NameError: global name 'n' is not defined

without from __future__ import nested_scopes, and with it I get

[ kalle aragorn ~ 01:00:00 ]$ python2.1 scopetest.py 
[0.14999999999999999, 0.56000000000000005, 0.17999999999999999]

no warning.  I have no idea how I'm supposed to get both warning and result,
except perhaps by using 2.2?
Please enlighten me.

Peace,
  Kalle
-- 
Free Dmitry Sklyarov! - http://www.freedmitry.org/