How to share variables between modules?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • snakyjake
    New Member
    • Jun 2009
    • 1

    How to share variables between modules?

    How do I share a module variable with another class module?
    Ideally I want file2.py to inherit the variables+value from file1.py.

    When I import a module, I want access to the variables (by reference), not a copy.

    Code:
    # file1.py
    import file2
    
    var1 = None
    
    def func1():
    	global var1
    	var1 = 'test'
    
    if __name__ == "__main__":
    	func1()
    	print var1  # prove variable was modified.
    	f2 = file2.ClassA()
    	f2.func3()
    Code:
    # file2.py
    import file1
    
    class ClassA:
    	def func3(self):
    		print file1.var1
    Code:
    Results:
    Test
    None
Working...