Hi.
Is it possible to exec() from within a function so the exec'd
code affects global?
For example
def foo():
exec ('X=[1,2,3]')
And I'd like 'X' to be a global variable.
Thanks,
Gerald 6 5494
Jerald wrote: Is it possible to exec() from within a function so the exec'd code affects global?
For example
def foo(): exec ('X=[1,2,3]')
And I'd like 'X' to be a global variable. x
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'x' is not defined def foo():
.... exec "x = [1, 2, 3]" in globals()
.... foo() x
[1, 2, 3]
Peter
You can do exec(some_code, globals(), globals()) to execute some_code as
if it were executed at module scope. I can't really think of a reason
off hand why you would want to do this, but I assume you have your
reasons.
On Sat, Sep 11, 2004 at 09:17:02PM -0700, Jerald wrote: Hi.
Is it possible to exec() from within a function so the exec'd code affects global?
For example
def foo(): exec ('X=[1,2,3]')
And I'd like 'X' to be a global variable.
Thanks,
Gerald
Hi too !
You can, also, to do :
def foo():
exec ('''global X
X=[1,2,3]''')
@-salutations
--
Michel Claveau
Hi !
I have one sample reason (only me ?) : i had make a COM-server in Python ;
and i send, from other applis, some Python's scripts, by a string, like
parameters. My COM-server can to execute this scripts.
*sorry for my bad english*
@-salutations
--
Michel Claveau
Michel Claveau - abstraction méta-galactique non triviale en fuite
perpétuelle. wrote: I have one sample reason (only me ?) : i had make a COM-server in Python ; and i send, from other applis, some Python's scripts, by a string, like parameters. My COM-server can to execute this scripts.
Similar reason here.
def mydef(X):
exec (X)
And
mydef("""
def g():
print 'Hi'
""")
cheers
Gerald This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jimmy Jim |
last post by:
Hey all, quick question to see if anyone has any ideas. I have an
object:
$special = new special;
Let's say I have a function:
class special {
function outputtext() {
return "test";
|
by: Jonathan |
last post by:
I'm puzzled by Python's behavior when binding local variables which
are introduced within exec() or execfile() statements. First, consider
this simple Python program:
# main.py
def f() :
x = 1
print "x:", x
f()
|
by: Nick Jacobson |
last post by:
This works fine:
x = 1
def execfunc():
print x
execfunc()
So why doesn't this?
s = \
|
by: kepes.krisztian |
last post by:
Hi !
In
Unifying types and classes in Python 2.2
article I see that:
>>> print a # show the result
|
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: N. Pourcelot |
last post by:
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):
|
by: Antoon Pardon |
last post by:
I have the following little piece of code:
class Cfg:pass
#config = Cfg()
def assign():
setattr(config, 'Start' , )
def foo():
config = Cfg()
|
by: Crutcher |
last post by:
I've been playing with dictionary subtypes for custom environments,
and I encountered a strange interaction between exec, dictionary
subtypes, and global variables. I've attached a test program, but
first I'd like to give some background.
Python uses dictionary objects as symbol tables in it's execution
contexts. Since these objects used to be basic types, which were not
subclassable, some of the interpreter code accessed them using...
|
by: comp.lang.tcl |
last post by:
set php {<? print_r("Hello World"); ?>}
puts $php; # PRINTS OUT <? print_r("Hello World"); ?>
puts
When I try this within TCL I get the following error:
|
by: carl.dhalluin |
last post by:
Hello
I am completely puzzled why the following exec code does not work:
mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)"
def execute():
exec mycode
execute()
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |