Python question

Christos TZOTZIOY Georgiou tzot at sil-tec.gr
Mon Sep 29 19:20:28 EDT 2003


On Mon, 29 Sep 2003 15:03:34 -0700, rumours say that "John D."
<lists at 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.




More information about the Python-list mailing list