why this error?

python.jiang python.jiang at 163.com
Sun Dec 16 10:25:00 EST 2007


hello friends, the question had show bellow, any friend can tell me why. thanks.
 
list:
def test():
  exec "import sys"
  a=range(15)
  b=[13,3]
  c=filter(lambda x: x not in b,a)
  return c
print test()
 
run result:
  File "a.py", line 2
    exec "import sys"
SyntaxError: unqualified exec is not allowed in function 'test' it contains a nested function with free variables
 
and must be change to bellow, then OK:
b=None
def testFilter(x):
  global b
  return x not in b
def test():
  exec "import sys"
  global b
  a=range(15)
  b=[13,3]
  c=filter(testFilter,a)
  return c
print test()
last run result is:
[0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14]
 
any friend can tell me why?
thanks alot very much!!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071216/1d8a46c9/attachment.html>


More information about the Python-list mailing list