473,320 Members | 1,863 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,320 software developers and data experts.

global variables shared across modules

Hello everyone

Consider the following two simple files. The first is my "program", the
other a file holding some global variable definitions and code shared
by this program and a "twin" program (not shown here):

program1.py:
------------

from shared import *

fun()

print "at top level: " + global_var
shared.py:
----------

global_var = "original value"

def fun():

global global_var

global_var = "changed value!"

print "inside fun(): " + global_var

return

------

When I run program1.py, I get

inside fun(): changed value!
at top level: original value!

How can I get the changed value to "persist" in such a way that it
isn't reset when control leaves fun()? Why is it even reset in the
first place? After all, the module has already been imported (and the
initialization of global_var executed) *before* fun() runs, right?

Any clarification and help in solving this would be great

Thanks

Mack

Sep 9 '05 #1
1 1685

MackS wrote:
print "inside fun(): " + global_var .... How can I get the changed value to "persist" in such a way that it
isn't reset when control leaves fun()? Why is it even reset in the
first place? After all, the module has already been imported (and the
initialization of global_var executed) *before* fun() runs, right?

Any clarification and help in solving this would be great


It _does_ persist and it _isn't_ reset. Try:

import shared
print shared.global_var

at the end of your program, and you will see that you have bound the
identifier global_var in your main program to the same immutable object
that the identifier global_var in shared was bound to, and then you
_changed_ the identifier global_var in shared to point to a different
object.

Sep 9 '05 #2

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

Similar topics

2
by: Ishwar Rattan | last post by:
Say I have two pythom modules in separate files: main.py -> contains main() modules and uses a global variable (say a) incr.py -> contains incre() that changes the value of global variable a...
7
by: Aaron Deskins | last post by:
I'm trying to write program with a main.py and several functions distributed in auxiliary *.py files. I've looked through the archives on the 'global' keyword, but I'm still not sure how to get...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
44
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
11
by: dotnetnoob | last post by:
in the old VB, you can use global variables to hold commonly use data. i'll like to pass a variables selected by user in the combobox, how do you hold this variable for other object to use. does...
23
by: Anil Gupte | last post by:
I am trying to set up a function that connects to the database that I can then use gloablly. I set up a class called L3Global in which I have a function as follows: Public Function...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.