why this error?

John Machin sjmachin at lexicon.net
Sun Dec 16 16:48:17 EST 2007


On Dec 17, 8:18 am, Joshua Kugler <jkug... at bigfoot.com> 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
>

or, given that you [Jiang the OP] are not using the sys module, omit
the whole statement!

The 'unqualified' exec means you have not specified a context by using
the 'in' keyword; as the manual says "the code is executed in the
current scope".

The 'nested function' is the lambda.

The 'free variable' is b ... if you do
    c = filter(lambda x: x not in [13, 3], a)
it "works".

The short story is that Python is not so wildly dynamic that you can
mix all of the above.

Is there something practical that you are trying to achieve that we
could help you with?



More information about the Python-list mailing list