The generalized use of global variables can make maintenance a nightmare because they do track the flow of your program, and sometimes you get a strange error because some module read the variable and acted on its value before any other module changed the value (and this may be the result of the inversion of two import operators in some unrelated third module). See also Wikipedia entry for global variables .
This is why you should avoid mutable global variables, IMO, and why Pylint gives a warning (and probably should issue more of them. Finding the use of the global is an easy way to identify some of them),
Make no mistake: I am not saying that you should not use global variables. Only you should avoid using them. Python has many legitimate cases for global variables. Until you get more pairs of W0603, you must make sure of this.
Now Logilab (the company supporting Pylint, and where I worked) once had to take care of the part> 50kloc of Python code with lots of duplication and 100 + mutable global variables. And that was hell.
Global variable solutions include:
- adding a parameter to functions that need to access a variable
- using class attributes
- using instance attributes (by passing the value that is needed for the class constructor)
- centralize mutable global values ββin the Configuration object, which is created so that it runs once at program startup (using environment variables, command line, configuration file ...) and never changes after that.
gurney alex
source share