470,821 Members | 1,962 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,821 developers. It's quick & easy.

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 22972
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

Post your reply

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

Similar topics

1 post views Thread by Mladen Gogala | last post: by
2 posts views Thread by Denis_dh | last post: by
reply views Thread by Leon Shaw | last post: by
2 posts views Thread by Asha Gill | last post: by
3 posts views Thread by Mothish K | last post: by
6 posts views Thread by tfsmag | last post: by
6 posts views Thread by awhan.iitk | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.