Python question

J

John D.

#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
 
C

Christos TZOTZIOY Georgiou

#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
 
S

Scott David Daniels

John said:
#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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,342
Members
48,018
Latest member
DelilahDen

Latest Threads

Top