473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Python question

#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
2 2856
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: Carl | last post by:
"Nine Language Performance Round-up: Benchmarking Math & File I/O" http://www.osnews.com/story.php?news_id=5602 I think this is an unfair comparison! I wouldn't dream of developing a numerical...
114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
50
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
158
by: Giovanni Bajo | last post by:
Hello, I just read this mail by Brett Cannon: http://mail.python.org/pipermail/python-dev/2006-October/069139.html where the "PSF infrastracture committee", after weeks of evaluation, recommends...
4
by: Martitza | last post by:
Hi. I work for a small company (actually in process of forming) interested in embedding or extending python as part of our commercial non-open-source product. We have legal counsel, but are...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.