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.
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
MyGlobalsand get an instance of it:You can access the object
gat any point in your script like this:But as noted in the comments, try to avoid global variables, and see whether you can make more use of OOP.