[Tutor] Re: Shadow error
Christopher Smith
csmith@blakeschool.org
Thu, 26 Jul 2001 22:34:51 -0500
Kalle Svensson <kalle@gnupung.net> wrote:
|
<cut>
|
| 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'
<cut>
|
| 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.
When I retried it (after having ended my session for the day) I, too, got
a traceback error and it wouldn't run at all. And I thought that I
noticed during my experimenting that sometimes I would get this error. I
suspect it has something to do with the environment becoming corrupted
after
making modifications. This behavior hasn't been consistent enough for
me to get a handle on. Maybe, too, I did a run where I didn't call the
function with problems but got the error. Like happens with the following:
Here's one of the other scripts that invokes the error. I just tried it
and
did not get a traceback but did get the warning:
<Untitled Script 5>:13: SyntaxWarning: local name 'm' in 'mtrx'
shadows use of 'm' as global in nested scope 'a'
def mtrx(m):
def a(i,j):
return m[i][j]
return a
l=[[1,2],[3,4]]
a=mtrx(l)
# no traceback unless I uncomment the next line:
# print a(1,2)
Here is a version that works without warnings.
def mtrx(l):
'''Returns a function which will accessess l[i][j] as l(i,j).
'''
def a(i,j,m=l):
return m[i][j]
return a
l=[[1,2],[3,4]]
a=mtrx(l)
print a(1,1)
# results in
# 4
/c