I was wondering if I declare a variable as global , can I share the same variable in a different file with the updated value. (ie) A global variable value to be used between different files without passing them within functions.
Say for eg.
Script 1 -
global foo = "sample"
-
-
def func1
-
global foo
-
foo = "updated sample"
-
print foo
-
-
print foo
-
func1
-
x = script2.script2()
-
print foo
-
Script 2 (filename - script2)
------------ -
class script2:
-
def __init__():
-
global foo
-
print foo
-
foo = "Updated in Script2 "
-
Is there a better way in which I use variables among different files without declaring them as global ?
Thanks
--V
8 23323 bartonc 6,596
Recognized Expert Expert
It works like this: - # script1.py
-
a = 1 # this is module scope variable
-
def func1():
-
pass # here a function in script1 is defined
-
-
class myClass(object):
-
pass # difine a class
- # script2.py
-
import script1
-
print script1.a
-
-
script1.func1()
-
-
myInst = script1.myClass()
bartonc 6,596
Recognized Expert Expert
It works like this: - # script1.py
-
a = 1 # this is module scope variable
-
def func1():
-
pass # here a function in script1 is defined
-
-
class myClass(object):
-
pass # difine a class
- # script2.py
-
import script1
-
print script1.a
-
-
script1.func1()
-
-
myInst = script1.myClass()
You can also (but this is discouraged) - # script2.py
-
from script1 import *
-
print a
or (this is better practice) - # script2.py
-
from script1 import myClass
-
myInst = myClass()
This is done to get constants, functions and classes. Actual variables always stay in the module where they are changed. This is part of what makes programming in python make sense. Globals are discourages as a generaly rule, but there are times when they are needed.
bartonc 6,596
Recognized Expert Expert
I was wondering if I declare a variable as global , can I share the same variable in a different file with the updated value. (ie) A global variable value to be used between different files without passing them within functions.
Say for eg.
Script 1 -
global foo = "sample"
-
-
def func1
-
global foo
-
foo = "updated sample"
-
print foo
-
-
print foo
-
func1
-
x = script2.script2()
-
print foo
-
Script 2 (filename - script2)
------------ -
class script2:
-
def __init__():
-
global foo
-
print foo
-
foo = "Updated in Script2 "
-
Is there a better way in which I use variables among different files without declaring them as global ?
Thanks
--V
By the way, welcome to the Python Forum on TheScripts.com. You'll learn to use code tags as you go along. There are a couple of places to find "Posting Guidelines". Just ask if you need help and keep posting,
Barton
I tried to implement based on the previous comment, but it doesn't seem to serve to work. I am not sure what am I missing here ?
The output I get is
['234', '123']
['444', '233']
[]
I am not sure how do I achieve this ?
If either of the files update that global variable, wherever I print, it should have that updated value. Also I am not sure declaring global variables is a nice idea for sharing variables among files.
Script 1
-------------- -
from foo import MyClass
-
-
myinst = MyClass()
-
print myinst.alist
-
myinst.alist = ['444','233']
-
print myinst.alist
-
myinst.func1()
Script 2
------------ -
alist = []
-
-
-
class MyClass(object):
-
alist = ['234','123']
-
pass
-
-
def func1(self):
-
print alist
-
pass
Thanks
bartonc 6,596
Recognized Expert Expert
I tried to implement based on the previous comment, but it doesn't seem to serve to work. I am not sure what am I missing here ?
I am not sure how do I achieve this ?
If either of the files update that global variable, wherever I print, it should have that updated value. Also I am not sure declaring global variables is a nice idea for sharing variables among files.
Thanks
Yes, declaring global variables is not a nice idea for sharing variables among files. You are on the right track by using classes to hold variables. Keep playing with that and keep posting.
bvdet 2,851
Recognized Expert Moderator Specialist
I tried to implement based on the previous comment, but it doesn't seem to serve to work. I am not sure what am I missing here ?
The output I get is
['234', '123']
['444', '233']
[]
I am not sure how do I achieve this ?
If either of the files update that global variable, wherever I print, it should have that updated value. Also I am not sure declaring global variables is a nice idea for sharing variables among files.
Script 1
-------------- -
from foo import MyClass
-
-
myinst = MyClass()
-
print myinst.alist
-
myinst.alist = ['444','233']
-
print myinst.alist
-
myinst.func1()
Script 2
------------ -
alist = []
-
-
-
class MyClass(object):
-
alist = ['234','123']
-
pass
-
-
def func1(self):
-
print alist
-
pass
Thanks
'alist' in Script2 is defined twice - first as a global variable and second as a class variable. 'func1' looks inside its scope first for alist. Not finding it, it looks in the global scope and finds an empty list '[]' to print. -
class MyClass(object):
-
alist = ['234','123']
-
pass
-
-
def func1(self):
-
print self.alist
-
pass
'func1' is now an instance method and finds the class variable 'alist' to print:
>>> ['234', '123']
['444', '233']
['444', '233']
I try to avoid global variables where possible.
bvdet 2,851
Recognized Expert Moderator Specialist
'alist' in Script2 is defined twice - first as a global variable and second as a class variable. 'func1' looks inside its scope first for alist. Not finding it, it looks in the global scope and finds an empty list '[]' to print. -
class MyClass(object):
-
alist = ['234','123']
-
pass
-
-
def func1(self):
-
print self.alist
-
pass
'func1' is now an instance method and finds the class variable 'alist' to print:
>>> ['234', '123']
['444', '233']
['444', '233']
I try to avoid global variables where possible.
Clarification - 'func1' now finds the class variable 'alist' to print. The global namespace for a function is always the module in which it is defined.
Script2: - alist = ['1', '2', '3']
-
-
class MyClass(object):
-
alist = ['234','123']
-
pass
-
-
def func1(self):
-
print self.alist
-
pass
>>> import Script2
>>> Script2
<module 'Script2' from 'C:\SDS2_7.0\ma cro\Work In Progress\Script 2.py'>
>>> Script2.alist
['1', '2', '3']
>>> myinst
<Script2.MyClas s object at 0x00D66FF0>
>>> myinst.alist
['444', '233']
>>> myinst.func1()
['444', '233']
>>>
HTH :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mladen Gogala |
last post by:
How can I share variables between two processes?
Here is my problem?
File test1.php:
-------------------------------------------------------------------
<?php
session_start();
$var1="This should";
$var2="be displayed";
$_SESSION=$var1;
|
by: Denis_dh |
last post by:
Hi,
I have a set of Forms and wish to have a varible used by more than one
of them but can't seem to figure where to initialise it or how to
acess it if I initialise it in a different form than the one that is
accessing it!
Any help appreciated!
Denis
|
by: Leon Shaw |
last post by:
I'm developing a user control which contain a web control label, in which I
want to share this label with other web forms the user control will be used.
How do I accomplish this mission?
Example: (for better understanding)
user control page (code behind VB)
public title as string
private sub page load....
Label1.text = title
|
by: Asha Gill |
last post by:
hello all
how can i share variables between asp.net pages without using the session or anything that has got to do with the global file. can i use properties for this? 1 page calling the other to accept the value
Asha Gill
|
by: Mothish K |
last post by:
Hello,
I am trying to connect 2 of my asp.net applications using context.items
collections
to share the variables.
but it says Could not load type 'Proj2.SignIn'.
I have set the authentication mode="Forms"
but still it says the same..
| |
by: darrel |
last post by:
This is something I should know, but I don't.
Say I have this:
Function
dim variable1
dim variable2
do stuff with the variables
|
by: tfsmag |
last post by:
here is some test code i've set up trying to figure out how to share
variables between two different methods.
__________________________________________________
Public Class test
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
|
by: Terry Olsen |
last post by:
What is the best practice for sharing variables across modules in the
same project?
I'm doing a console app with different modules for different functions
(file i/o, sql commands, string manipulation, etc). But I have
variables in the main module that other modules need to access such as
AppPath, etc.
What is the "best practice" concerning a situation like this?
|
by: awhan.iitk |
last post by:
I have a set of variables that I want to share across mulitple c++
files. I was using the extern method so far. Is there any other way to
do the same. The variables are not constants and I get the values
during run time.
|
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:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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.
| |