Global variables in Python across all modules

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I am currently working on a game project in Python, and I find myself struggling with the "concepts" behind Python as I m used to languages such as C or Java.

I have tried to "divide" my project in several files (that are called modules if I m not mistaken, although they are all in the same folder as I would do with C source files) with different themes : objects.py, map.py, rendering.py, etc. They contain different classes and functions related to these notions. I have a main.py that contains all my "main" functions (such as new_game and so on) as well as the first "call", that will call main_menu.

I also have a constants.py file where I store constant values. Now the thing is, I have several "global" variables and objects in my game : the player object, the map, and so on. I wish to keep these variables global across all modules and be able to access and modify them from everywhere.

I first created a global_variables.py file where I listed all my variables (such as map = None, player = None, etc), expecting to modify them via other modules.

The problem is, each time I called import global_variables again, it ran through the whole code and reinitialized all of them to None, which I obviously do not want.

I read on another stackoverflow thread a tip: creating a init() function in my file where I would initialize all variables and declare them global. This way, I call global_variables.init() once, and then I can simply do global_variables.player to access the player and modify it.

It works well, but I find it cluttered. I have a ton of global foo, followed by foo = None and it feels like unnecessary lines of code. Furthermore, IDEs can t "access" the variables, when I type global_variables. , it does not suggest the variables as autocomplete, which is quite a pain.

So is there another way of doing so? The idea would be to "declare" the variables out of any function so that the IDEs understand they re there, but only "initialize" them once so that I avoid them being reset each time I import the global_variables module.

Answers

The problem is, each time I called import global_variables again, it ran through the whole code and reinitialized all of them to None, which I obviously do not want.

No, Python will not re-run the code when you import that module multiple times. A module is executed just once , producing a module object that then is reused for subsequent imports.

As such, using a module for all your program globals is fine. Just make sure you import the module and assign back to attributes on that module object:

import global_variables

global_variables.name =  Inigo Montoya 

and any other module that imports global_variables will see global_variables.name as set to Inigo Montoya . No init() function required.

Do not use from global_variables import name, because that creates a new name name in your module that won t see any new values if other code rebinds global_variables.name to a different value.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/26236987/global-variables-in-python-across-all-modules

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils