Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > strange SyntaxError

Reply
Thread Tools

strange SyntaxError

 
 
Attila Szabo
Guest
Posts: n/a
 
      02-25-2005
Hi,

I wrote this sample piece of code:

def main():
lambda x: 'ABC%s' % str(x)
for k in range(2): exec('print %s' % k)

main()

With the lambda line, I get this:
SyntaxError: unqualified exec is not allowed in function 'main'
it contains a nested function with free variables
Without the lambda, it's ok...

What's this ?

thanks

--

A t t i l a :: http://www.velocityreviews.com/forums/(E-Mail Removed) :: S z a b o

 
Reply With Quote
 
 
 
 
Scott David Daniels
Guest
Posts: n/a
 
      02-25-2005
Attila Szabo wrote:
> Hi,
> def main():
> lambda x: 'ABC%s' % str(x)
> for k in range(2): exec('print %s' % k)

OK, to no real effect, in main you define an unnamed function that
you can never reference. Pretty silly, but I'll bite.

Next you run run a loop with exec looking like you think it is a
function. In the loop, your "exec" statement does not control
(specify) its access to global and local variables. This could be
a problem if your function were different. The function above does
not do any up-referencing, but:

def main():
def inner():
return k + 1

for k in range(4):
exec 'k = 3 * k'
print k, inner()

What should this do?

If you think you know, how about replacing the exec line with:

exec raw_input('Tricky: ')

You can fix this by controlling what variables the exec can write:

exec 'k = 3 * k' in globals(), locals()

This is why locals() does not do write-back.

The rule below (which was high on the list when searching for exec),
tells the exact rules, the above stuff is just why.

http://www.python.org/doc/2.4/ref/dynamic-features.html

--Scott David Daniels
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Attila Szabo
Guest
Posts: n/a
 
      02-26-2005
2005, Feb 25 -> Scott David Daniels wrote :
> Attila Szabo wrote:
> >Hi,
> >def main():
> > lambda x: 'ABC%s' % str(x)
> > for k in range(2): exec('print %s' % k)

> OK, to no real effect, in main you define an unnamed function that
> you can never reference. Pretty silly, but I'll bite.


This code was simplified, the lambda was part of a map,
and the exec line is different too, I just wrote
this to picture the situation, but it's ok now...
At first I was surprised, but I read a bit and it's clear now...

Thanks
--

A t t i l a :: (E-Mail Removed) :: S z a b o
 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      02-26-2005
Attila Szabo wrote:
> 2005, Feb 25 -> Scott David Daniels wrote :
>>Attila Szabo wrote:
>>... lambda x: 'ABC%s' % str(x) ...
>>OK, to no real effect, in main you define an unnamed function that
>>you can never reference. Pretty silly, but I'll bite.


> This code was simplified, the lambda was part of a map, ...


I'm sorry, upon re-reading my reply, I see how it looks like a
snide comment. I apologize. I didn't mean to malign you, and
I did indeed appreciate that the code was obviously boiled down.
I just thought:
def named(x):
return 'ABC%s' % str(x)
provides a function name to talk about, exhibits the same problem,
and is "simpler".

--Scott David Daniels
(E-Mail Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a strange SyntaxError CoolGenie Python 5 12-10-2007 10:21 AM
SyntaxError: invalid syntax DIBS Python 5 12-27-2005 06:20 PM
call static function from extension module - syntaxerror Todd Python 1 12-23-2005 06:12 AM
Windows Python 2.4: Unbuffered flag causes SyntaxError on interactive sessions? Lonnie Princehouse Python 11 09-15-2005 09:14 AM
SyntaxError: unqualified exec is not allowed in ... ? j vickroy Python 1 10-08-2003 05:10 PM



Advertisments