On Wed, Jul 11, 2012 at 10:30 AM, Chris Hare <ch...@labr.net> wrote: > > 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? > > > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor >
You are making this too complicated. I think you are getting hung up on instantiating objects. You don't need to instantiate an object everytime you use it. Once you have it, it's a variable like any other and you can pass it around. Just have a class body with your variables: class MyObj(object): def __init__(self, *args, **kwargs): do stuff with variables here Then instantiate the object m = myobject(pass in your parameters) now pass around the instantiated object on an as needed basis, or, access the instance directly example 1: def func(obj): do stuff with obj to call it: func(m) example 2: File 1: #tests def reset_m(m, settings): m.settings = settings File 2: #test_two class MyObj(object): def __init__(self, settings=True): self.settings = settings if __name__ == '__main__': m = MyObj() File 3: #main import test_two import tests m = test_two.MyObj() print m.settings tests.reset_m(m, False) print m.settings The above will print True, False But, you don't need to create a class that has a single attribute, which is a dictionary. In that case, you can just create a dict and pass that around (I generally prefer to have a class body and access things using dot notation rather than dictionary syntax) Generally though, I think this entire idea of settings dictionary or class can be simplified. I'll bet most of the settings have some general similarities or are repeated, or they are the same for each user, each time. In that case, you should probably move to a more database driven approach, or hardcode settings in a file and override those hardcoded settings on an as needed basis. In django, you have a "settings" file. In there, you hard code some most of your settings, like apps you are using, where your database resides, middleware, etc.
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor