Question

I'd like to change the storage of all globals in a python script, as if I could just write use_this_dict_for_globals(some_dict).

They're all prefixed with "g_" so if I could replace "g_" with "some_dict." to change all those variable accesses, this would work too. I'd actually prefer this way, but in Python I'd either need to use variable accesses like foo['bar'] and not foo.bar, or I need appearently some python class stuff (?) that I know nothing about.

Is there any quick/reliable way to do it?

Update: As all answers state, you can use a class instance like a dictionary, with no restrictions, foo.bar = 123. You just can't use this notation with dictionaries (like you can do in Lua with tables), which lead me to think there would be no way at all. Thanks for your help.

Was it helpful?

Solution

You can use this "python class stuff" to implement one object holding all your global variables. Note that I do not think that this is a good approach in general, but it comes very close to what you intended to do.

Near the beginning of your script, define a class MyGlobals and get an instance of it:

class MyGlobals(object):
    pass
g = MyGlobals()

You can access the object g at any point in your script like this:

g.foo = "foo"
print g.foo

But as noted in the comments, try to avoid global variables, and see whether you can make more use of OOP.

OTHER TIPS

If you have variables in the global namespace named as g_var1 and g_var2 (very bad by the way), you can access them with:

some_dict = {varname[2:]: var for varname, var in globals().iteritems() if varname.startswith('g_')}

You will get a dict some_dict with all the global variables that start with g_. You can access these with some_dict['var1'].

But don't do this unless you absolutely have to. Use classes and modules/packages. Simple example:

class SomeClass(object):
    def __init__(self):
        self.var1 = 'whatever'
        self.var2 = 'whatever also'

c = SomeClass()
print c.var1, c.var2

For more information, read about classes and modules (import some_module and stuff like that).

Conclusion:

Don't prefix with g_ your global variables, use one dictionary in the global namespace and access these from there or your own class.

Good luck.

I would break out everything out into modules, and stay away from and thing global.

Have one module that has all of your global vars, and then pass an instance of this to other modules that need it.

class doSomeThing(object):
    def __init__(self, config):
        self.config = config

    def some_func(self):
        # something
        pass

class Global_Config(object):
    def __init__(self, params):
        # load global information

if __name__ == "__main__":
    # construct the config
    config = Global_Config(some params)
    one = doSomething(config)
    two = doSomethingTwo(config)
    # do stuff with do some work with one and two and config
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top