473,387 Members | 1,779 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,387 software developers and data experts.

strange SyntaxError

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 :: tr***@ugyvitelszolgaltato.hu :: S z a b o

Jul 18 '05 #1
3 1940
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
Sc***********@Acm.Org
Jul 18 '05 #2
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 :: tr***@ugyvitelszolgaltato.hu :: S z a b o
Jul 18 '05 #3
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
Sc***********@Acm.Org
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Steve Menard | last post by:
I have a strange problem with Python command-line interactive mode. Problem has been observed on every 2.3 version where it was tested, from 2.3 alpha to 2.3.4 Test is run on windows XP and 2000...
1
by: Todd | last post by:
Hi, I'm working right from the example here to make a basic extenstion module. http://docs.python.org/ext/intro.html...
5
by: DIBS | last post by:
I'm new to Python and I don't understand what I'm doing wrong. I'm running windows xp. In the command line window, I type: Python Sudoku.py and I get the response" SyntaxError: invalid...
3
by: Brian Blais | last post by:
Hello, I have an odd kind of Heisenbug in what looks like a pretty simple program. The program is a progress bar code I got at the Python Cookbook: ...
5
by: joe.hrbek | last post by:
Hello everyone, I'm experimenting with python and i'm following this tutorial: http://docs.python.org/tut/node6.html#SECTION006400000000000000000 I'm in section 4.7.5 Lambda Forms. In this...
10
by: ronrsr | last post by:
no matter where I place this imported file,the statement after it in the main program gets a syntax error, regardless of the syntax. I think I may have changed something in this file, but I'm...
7
by: oscartheduck | last post by:
I have a small script for doing some ssh stuff for me. I could have written it as shell script, but wanted to improve my python skills some. RIght now, I'm not catching a syntax error as I'd...
5
by: riverhorus | last post by:
I'm new to Python. I got this Python script from a friend. When I run it, I get an error message for line 7, "return None" saying "SyntaxError: 'return' outside function" Can someone explain what...
12
Dormilich
by: Dormilich | last post by:
Hi, I’ve encountered quite a strange error when loading one of my scripts in Safari (4.0.3/Mac): I have no idea, what Safari is complaining about, since it works in FF 3.5 and Opera 10, does...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.