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

sharing variables between two scripts

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 23298
bartonc
6,596 Expert 4TB
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 Expert 4TB
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 Expert 4TB
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
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 Expert 4TB
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 Expert Mod 2GB
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 Expert Mod 2GB
'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\macro\Work In Progress\Script2.py'>
>>> Script2.alist
['1', '2', '3']
>>> myinst
<Script2.MyClass object at 0x00D66FF0>
>>> myinst.alist
['444', '233']
>>> myinst.func1()
['444', '233']
>>>

HTH :)
Feb 6 '07 #8
Thanks .. that helped.
Feb 6 '07 #9

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

Similar topics

1
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...
2
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...
0
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? ...
2
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...
3
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...
3
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
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...
1
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...
6
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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...
0
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...
0
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,...
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.