Hello,
I can't understand some specific behaviour of the exec statment.
For example, say that I create such a class A :
class A:
def __init__(self):
self.n = 3
self.m = None
def h(self, ini):
n = self.n
m = self.m
if ini: exec("def m(x): return n+x"); self.m=m
else: m(7)
Now :
obj = A()
obj.h(1)
obj.h(0)
I get :
Traceback (most recent call last):
File "<input>", line 1, in ?
File "<input>", line 9, in h
File "<string>", line 1, in m
NameError: global name 'n' is not defined
Now, suppose I would like to make exactly the same without exec :
class A:
def __init__(self):
self.n = 3
self.m = None
def h(self, ini):
n = self.n
m = self.m
if ini:
def m(x): return n+x
self.m=m
else: return m(7)
je lance :
obj = A()
obj.h(1)
obj.h(0)
This time, it works fine !!!
If I knew why the first doesn't work, and the second does, it would be a
great help for me for my program...
Thank you very much ! 2 1439
N. Pourcelot wrote: I can't understand some specific behaviour of the exec statment.
For example, say that I create such a class A :
class A: def __init__(self): self.n = 3 self.m = None def h(self, ini): n = self.n m = self.m if ini: exec("def m(x): return n+x"); self.m=m else: m(7)
Now : obj = A() obj.h(1) obj.h(0)
I get :
Traceback (most recent call last): File "<input>", line 1, in ? File "<input>", line 9, in h File "<string>", line 1, in m NameError: global name 'n' is not defined
exec only supports local and global scopes; the "n" inside the exec statement
is a not a local, so it's assumed to be a global variable.
(Python's lexical scoping requires the compiler to look for free variables in inner
scopes before generating code for the outer scope; it cannot do that for exec, for
obvious reasons).
</F>
Thanks for you reply.
I added locals() to my exec statment, so it worked, but I wasn't so sure
about my interpretation of the problem.
Fredrik Lundh wrote: N. Pourcelot wrote:
I can't understand some specific behaviour of the exec statment.
For example, say that I create such a class A :
class A: def __init__(self): self.n = 3 self.m = None def h(self, ini): n = self.n m = self.m if ini: exec("def m(x): return n+x"); self.m=m else: m(7)
Now : obj = A() obj.h(1) obj.h(0)
I get :
Traceback (most recent call last): File "<input>", line 1, in ? File "<input>", line 9, in h File "<string>", line 1, in m NameError: global name 'n' is not defined
exec only supports local and global scopes; the "n" inside the exec statement is a not a local, so it's assumed to be a global variable.
(Python's lexical scoping requires the compiler to look for free variables in inner scopes before generating code for the outer scope; it cannot do that for exec, for obvious reasons).
</F> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: James Goldwater |
last post by:
I have a situation where I actually _do_ want the command window to
show after a runtime.exec() call. (Although we pipe stout/stdin to
our own logging mech, the program needs to be able to trap...
|
by: Benoit Dejean |
last post by:
hello, i have a question about forking processes
atm, i have some code which i want to rewrite
os.system("cd ~ && exec " + cmd + " & disown")
i want to remove this os.system call
|
by: Andr? Roberge |
last post by:
I have the following two files:
#--testexec.py--
def exec_code(co):
try:
exec co
except:
print "error"
#-- test.py--
|
by: Nick Coghlan |
last post by:
<Sorry for the disconnected reply - I've been having news server issues, and
just switched to the mailing list instead>
Andr? Roberge wrote:
> I don't understand why I need to use the global...
|
by: Stefan Seefeld |
last post by:
hi there,
I have trouble running some python code with 'exec':
t.py contains:
class Foo: pass
class Bar:
f = Foo
From a python shell I do:
|
by: Jan |
last post by:
I store sql-commands in a database table. In the first step I get the
sql command out of the database table with embedded sql. In the second
step I try to execute the command, which i got from the...
|
by: comp.lang.tcl |
last post by:
The TCL command I am using will do a command-line action on a PHP
script:
set cannotRunPHP
I have to do it this way as both the TCL script and the PHP script run
as CLI. However, "info.php"...
|
by: findu_2005 |
last post by:
Hi,
When I run the following command on sql server:
exec ('print "OK"')
go
This message appears:
Server: Msg 128, Level 15, State 1, Line 1
|
by: ajh |
last post by:
Using Ubuntu 5, Apache2 and PHP 4 I am trying display in a web browser
the output from a program I wrote in C.
Here is an abridged version of my code (I can post all of it later):
<?php
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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: 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...
|
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,...
| |