why this error?

Larry Bates larry.bates at websafe.com
Sun Dec 16 19:21:42 EST 2007


Joshua Kugler wrote:
> python.jiang wrote:
> 
>> 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
> 
> I have no reason why you're getting the error, but there is no reason to try
> to exec an import.  Just use
> 
> import sys
> 
> Hope that helps.
> 
> j
> 

Don't exec "import sys" just do import sys.

Also.

a=range(15)
b=[13,3]
c=filter(lambda x: x not in b,a)

is just:

c=[x for x in xrange(15) if x not in [13,3]]

IMHO this is much easier to read.

-Larry



More information about the Python-list mailing list