473,804 Members | 3,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 18 '05 #1
3 1956
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('Tric ky: ')

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***********@A cm.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***@ugyvitels zolgaltato.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***********@A cm.Org
Jul 18 '05 #4

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

Similar topics

0
1305
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 I have written a ptyhon extension module called jpype, and run the following code D:\>python
1
1671
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 http://www.dalkescientific.com/writings/diary/archive/2005/04/26/extending_python.html I can load my module into python and dir shows my function. But I get a syntax error if I try to access it.
5
20353
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 syntax
3
1365
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: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/168639 (including the code below)
5
1237
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 section I was working along and I noticed something strange. It happened because of a typo. Below is a copy/paste from my idle session: >>>def make_incrementor(n): return lambda x: x+n
10
6422
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 stuck. Can anyone help? #!/usr/local/bin/python # Copyright 2004 by Stephen Masterman #Change the db connection details here.
7
9892
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 like to. Here's my code: #!/usr/bin/env python import sys
5
23907
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 the SyntaxError is and how to fix it? Here's the block of script. The error occurs in line 7. def PC1(key, src, decryption=True): sum1 = 0; sum2 = 0; keyXorVal = 0; if len(key)!=16: print “Bad key length!”
12
5654
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 anyone of you have an idea? thanks, Dormi // by Gavin Kistner
0
10562
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10319
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10303
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10070
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.