Exec woes

Rhodri James rhodri at wildebst.demon.co.uk
Wed Jan 28 23:12:07 EST 2009


On Wed, 28 Jan 2009 07:47:00 -0000, Hendrik van Rooyen  
<mail at microcorp.co.za> wrote:

> Stephen Hansen wrote:
>
>> Exec is a statement, not a function nor an object: even though you can  
>> enclose parens around its arguments like you do later on, they
>> don't have any syntax meaning
>
> This is actually not correct - it is the root cause of my trouble.
> if you write, in a nested scope:
>
> exec ( "somestring to execute" in globals(),locals())
>
> You get the syntax error, as the interpreter somehow sees it as one,  
> unqualified thing.

Well, no.  Look at the error Python gives you, nested scope or not:

Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: exec: arg 1 must be a string, file, or code object

If exec is a function, arg 1 is the boolean expression
   "somestring to execute" in globals()
which is unlikely to be what you want.  If exec is a statement,
arg 1 is a tuple of two elements,
   "somestring to execute" in globals()
and
   locals()
which is also unlikely to be what you want.  Neither of these are
giving you a string, file or code object, exactly as the interpreter
is telling you.

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list