473,404 Members | 2,137 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,404 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 2861
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.