473,946 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I exec() in global?

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
Jul 18 '05 #1
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

Jul 18 '05 #2
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

Jul 18 '05 #3
Hi too !

You can, also, to do :

def foo():
exec ('''global X
X=[1,2,3]''')

@-salutations
--
Michel Claveau

Jul 18 '05 #4
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

Jul 18 '05 #5
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
Jul 18 '05 #6
re-Hi !

http://ponx.org (IE only, with scripts autorized)
Jul 18 '05 #7

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

Similar topics

1
2476
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";
2
17429
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()
5
1872
by: Nick Jacobson | last post by:
This works fine: x = 1 def execfunc(): print x execfunc() So why doesn't this? s = \
3
3955
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
1
2243
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--
2
1486
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):
10
409
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()
1
2717
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...
21
7895
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:
4
2979
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()
0
10162
marktang
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...
0
9981
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,...
0
11153
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
8253
isladogs
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...
0
7424
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
6331
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4941
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
2
4533
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3541
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.