473,404 Members | 2,137 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,404 software developers and data experts.

execfile in global scope

I need to execfile() from a function in order to set value for a global
variable from inside the executed file. I know there are "globals" and
"locals" optional arguments for execfile, but I just can't figure out
how to use them correctly. Here is an example:

Change.py
=========
x = 555

Main.py
=======

def changevar():
execfile("change.py")

x = 111 # global var
changevar()
print x # returns 111 instead of 555

Aug 24 '05 #1
3 5608
What about :

globdict= globals()

def changevar():
global globdict
execfile("changevar.py",globdict)

x = 111 # global var
changevar()
print x # returns 111 instead of 555

Aug 24 '05 #2
What about :

globdict= globals()

def changevar():
global globdict
execfile("changevar.py",globdict)

x = 111 # global var
changevar()
print x # returns 111 instead of 555

Aug 24 '05 #3
On 24 Aug 2005 01:50:25 -0700, v.*****@gmail.com wrote:
I need to execfile() from a function in order to set value for a global
variable from inside the executed file. I know there are "globals" and
"locals" optional arguments for execfile, but I just can't figure out
how to use them correctly. Here is an example:

Change.py
=========
x = 555

Main.py
=======

def changevar():
execfile("change.py") execfile("change.py", globals()) # the builtin globals function returns
# the current module's global namespace
# as a writable dict
x = 111 # global var
changevar()
print x # returns 111 instead of 555

open('change.py','w').write("""\ ... x = 555
... """) print '%s\n%s%s' %('-'*30, open('change.py').read(), '-'*30) ------------------------------
x = 555
------------------------------ dir() ['__builtins__', '__doc__', '__name__'] x = 111
x 111 def changevar(): ... execfile('change.py', globals())
... x 111 changevar()
x

555

Regards,
Bengt Richter
Aug 24 '05 #4

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

Similar topics

2
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...
2
by: overly.crazy.steve | last post by:
I am seeing something strange with execfile. I've simplified the code to: ########## t.py ########## print "here" v = None def f(): global v v = 6
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
1
by: Fernando Perez | last post by:
Hi all, I'm finding the following behavior truly puzzling, but before I post a bug report on the site, I'd rather be corrected if I'm just missing somethin obvious. Consider the following...
2
by: Dave Westerman | last post by:
I've got a Jython script where I'm doing an execfile() to pull in a small script that contains a function, which I then call. execfile(app_applscript) optionList = createOptionList(applOptions)...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
1
by: moijes12 | last post by:
Hi i have 3 python files and i want to execute the files sequentially using the execfile command.Hence ,i have written the following program fileList = for fileName in fileList :...
2
by: Igor Kaplan | last post by:
Hello python gurus. I got quite unusual problem and all my searches to find the answer on my own were not successful. Here is the scenario: I have the python program, let's call it script1.py,...
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: 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
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
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,...
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
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...

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.