By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,922 Members | 2,008 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,922 IT Pros & Developers. It's quick & easy.

Python question

P: n/a
#This program gives: "SyntaxError: unqualified exec is not allowed
# in function _ it contains a nested function with free variables"

#I understand this is a scope problem(?)
#I don't understand why this fails. It should be perfectly obvious
#what z is equal to when exec sees it. Furthermore, it shouldn't care
#about any other variables since it uses no others, and writes to none.

#What does this mean? What is "unqualified" and how do I qualify it?
#Are the free variables d or z? Is 'free' a boolean state or are there
#other related qualities?
#What are the various work-arounds?

#I am using d as a global dictionary to store all my variables.

d={} #Doesn't matter if I have d here
def myfunc():
z='print'
exec(z)
d={} #or here...
def setvar():
d[0]=1
return

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 29 Sep 2003 15:03:34 -0700, rumours say that "John D."
<li***@webcrunchers.com> might have written:
#This program gives: "SyntaxError: unqualified exec is not allowed
# in function _ it contains a nested function with free variables"

#I understand this is a scope problem(?)
#I don't understand why this fails. It should be perfectly obvious
#what z is equal to when exec sees it. Furthermore, it shouldn't care
#about any other variables since it uses no others, and writes to none.

#What does this mean? What is "unqualified" and how do I qualify it?
Use 'exec <something> in <globals_dic>, <locals_dic>' to 'qualify' the
exec, otherwise it uses the locals of the function. See
http://www.python.org/doc/ref/exec.html .
#Are the free variables d or z?
d is the free var (it's used by the setvar nested function). It doesn't
matter if you don't reference it in the exec statement, because it can
be accessed by the locals() dict whose use is implied by the
'unqualified' exec statement.
Is 'free' a boolean state or are there
#other related qualities?
A variable is either free or not; so you can call 'free' a boolean
state, but this is an abstraction about which you shouldn't care. See
http://www.python.org/doc/ref/naming.html
#What are the various work-arounds?
Already answered.
#I am using d as a global dictionary to store all my variables.

d={} #Doesn't matter if I have d here
# it doesn't
def myfunc():
z='print'
exec(z)
d={} #or here...
# it does, because of the next two lines
def setvar():
d[0]=1
return

--
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.
Jul 18 '05 #2

P: n/a
John D. wrote:
#This program gives: "SyntaxError: unqualified exec is not allowed
# in function _ it contains a nested function with free variables"

Here is a (slightly) smaller version that may give you an idea:
def myfunc(codetext):
def setvar():
d[0]=1
exec codetext
### Causes the error
def myfunc(codetext):
def setvar():
d[0]=1
exec codetext in globals()
### Does not cause the error

The complaint is about the ambiguity of z's execution environment
(globals and locals), when there is code (setvar) which will reference
the local environment. Notice that:
def myfunc(codetext):
def setvar(d):
d[0]=1
exec codetext
has no problem: setvar now only looks at its own code. The question
is not what setvar looks at outside of itself (enclosing function or
globals), but whether it can (potentially) look at any of myfunc's
locals. If setvar looks at nothing, it doesn't matter whether code
in z potentially creates a local in myfunc or not.

Hope this is enough for you to figure it out. If not, ask a few more
questions.

-Scott David Daniels
Sc***********@Acm.Org

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.