[Tutor] advice on global variables

Chris Hare chare at labr.net
Wed Jul 11 16:30:33 CEST 2012


On Jul 11, 2012, at 8:05 AM, Walter Prins wrote:

> [snip]

> Your original example modified as demonstration:
> 
> a.py:
> ====
> import shared
> import b
> 
> def func1():
>        print "global var in func1 = %s" % shared.global_var
> 
> class intclass:
>        def func2(self):
>                print "global var in intclass = %s" % shared.global_var
> 
> print "global_var = %s" % shared.global_var
> func1()
> f = intclass()
> f.func2()
> g = b.extclass()
> g.func3()
> 
> b.py:
> ====
> 
> import shared
> 
> class extclass:
>        def func3(self):
>                print "global var in extclass = %s" % shared.global_var
> 
> 
> shared.py:
> =======
> global_var = "global"
> 
> 
I like where this is going Walter.  I guess where I am confused is this:

the globals are not static - they are set once and then won't change during the lifetime of the user's session.   

So, after messing around with the ram DB idea, I realized I was back to the same problem.

Let's say I create a dictionary to store all of the "globals" and other data I want available everywhere and I put that in a class.  Every time I create an instance of the class to be able to access the data, a new instance of the dictionary is created that loads the same data in it.  Seems kinda inefficient for me, especially if the data changes in one instance - how do you keep the others all in sync, or does that just happen automatically?

I think there is something that I fundamentally not understanding and I don't know what it is.   Am I making this too complicated?  





More information about the Tutor mailing list