473,657 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sharing variables between two scripts

7 New Member
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
Expand|Select|Wrap|Line Numbers
  1. global foo = "sample"
  2.  
  3. def func1
  4.    global foo
  5.    foo = "updated sample"
  6.    print foo 
  7.  
  8. print foo
  9. func1 
  10. x = script2.script2()
  11. print foo
  12.  
Script 2 (filename - script2)
------------
Expand|Select|Wrap|Line Numbers
  1. class script2:
  2.      def __init__():
  3.           global foo
  4.           print foo
  5.           foo = "Updated in Script2 "
  6.  
Is there a better way in which I use variables among different files without declaring them as global ?

Thanks
--V
Feb 6 '07 #1
8 23323
bartonc
6,596 Recognized Expert Expert
It works like this:

Expand|Select|Wrap|Line Numbers
  1. # script1.py
  2. a = 1 # this is module scope variable
  3. def func1():
  4.     pass    # here a function in script1 is defined
  5.  
  6. class myClass(object):
  7.     pass    # difine a class
Expand|Select|Wrap|Line Numbers
  1. # script2.py
  2. import script1
  3. print script1.a
  4.  
  5. script1.func1()
  6.  
  7. myInst = script1.myClass()
Feb 6 '07 #2
bartonc
6,596 Recognized Expert Expert
It works like this:

Expand|Select|Wrap|Line Numbers
  1. # script1.py
  2. a = 1 # this is module scope variable
  3. def func1():
  4.     pass    # here a function in script1 is defined
  5.  
  6. class myClass(object):
  7.     pass    # difine a class
Expand|Select|Wrap|Line Numbers
  1. # script2.py
  2. import script1
  3. print script1.a
  4.  
  5. script1.func1()
  6.  
  7. myInst = script1.myClass()
You can also (but this is discouraged)
Expand|Select|Wrap|Line Numbers
  1. # script2.py
  2. from script1 import *
  3. print a
or (this is better practice)
Expand|Select|Wrap|Line Numbers
  1. # script2.py
  2. from script1 import myClass
  3. 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.
Feb 6 '07 #3
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
Expand|Select|Wrap|Line Numbers
  1. global foo = "sample"
  2.  
  3. def func1
  4.    global foo
  5.    foo = "updated sample"
  6.    print foo 
  7.  
  8. print foo
  9. func1 
  10. x = script2.script2()
  11. print foo
  12.  
Script 2 (filename - script2)
------------
Expand|Select|Wrap|Line Numbers
  1. class script2:
  2.      def __init__():
  3.           global foo
  4.           print foo
  5.           foo = "Updated in Script2 "
  6.  
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
Feb 6 '07 #4
vishalsethia
7 New Member
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
--------------
Expand|Select|Wrap|Line Numbers
  1. from foo import MyClass
  2.  
  3. myinst = MyClass()
  4. print myinst.alist
  5. myinst.alist = ['444','233']
  6. print myinst.alist
  7. myinst.func1()


Script 2
------------
Expand|Select|Wrap|Line Numbers
  1. alist = []
  2.  
  3.  
  4. class MyClass(object):
  5.         alist = ['234','123']
  6.         pass
  7.  
  8.         def func1(self):
  9.                 print alist
  10.                 pass


Thanks
Feb 6 '07 #5
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.
Feb 6 '07 #6
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
--------------
Expand|Select|Wrap|Line Numbers
  1. from foo import MyClass
  2.  
  3. myinst = MyClass()
  4. print myinst.alist
  5. myinst.alist = ['444','233']
  6. print myinst.alist
  7. myinst.func1()


Script 2
------------
Expand|Select|Wrap|Line Numbers
  1. alist = []
  2.  
  3.  
  4. class MyClass(object):
  5.         alist = ['234','123']
  6.         pass
  7.  
  8.         def func1(self):
  9.                 print alist
  10.                 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.
Expand|Select|Wrap|Line Numbers
  1. class MyClass(object):
  2.         alist = ['234','123']
  3.         pass
  4.  
  5.         def func1(self):
  6.                 print self.alist
  7.                 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.
Feb 6 '07 #7
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.
Expand|Select|Wrap|Line Numbers
  1. class MyClass(object):
  2.         alist = ['234','123']
  3.         pass
  4.  
  5.         def func1(self):
  6.                 print self.alist
  7.                 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:
Expand|Select|Wrap|Line Numbers
  1. alist = ['1', '2', '3']
  2.  
  3. class MyClass(object):
  4.         alist = ['234','123']
  5.         pass
  6.  
  7.         def func1(self):
  8.                 print self.alist
  9.                 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 :)
Feb 6 '07 #8
vishalsethia
7 New Member
Thanks .. that helped.
Feb 6 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2732
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;
2
1662
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
0
1230
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
2
1823
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
3
1334
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..
3
1734
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
6
1539
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 "
1
1597
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?
6
1727
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.
0
8411
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...
1
8513
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,...
0
8613
tracyyun
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...
0
7351
agi2029
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...
1
6176
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
5638
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
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
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
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.