Python Global Keyword vs. Pylint W0603

Pylint W0603 states:

Using a global operator. Used when you use the expression "global" to update a global variable. PyLint is simply trying to recapture this use. This does not mean that you cannot use it!

I wonder why this is so. Is there any other Pythonic way to change immutable, modular variables inside a function? Is it recommended to pack them inside mutable variables, like dictionaries? Or perhaps turn the whole module into a class?

In my opinion, this warning should disappear if the variable is suggested to be "private" (prefixed with _ or __).

+8
python global-variables pylint global
source share
3 answers

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.
+16
source share

I would replace this:

 the_file = None def open_the_file(fname): global the_file the_file = open(fname) def write_to_the_file(data): the_file.write(data) open_the_file("boo") write_to_the_file("Hi!") 

with this:

 class FileProgram(object): def __init__(self): self.the_file = None def open_the_file(fname): self.the_file = open(fname) def write_to_the_file(data): self.the_file.write(data) if __name__ == "__main__": prog = FileProgram() prog.open_the_file("boo") prog.write_to_the_file("Hi!") 

You could say: "This is too difficult for my simple task!" Well, then do not run pylint in your program. You cannot ask that the peelint understands that your program is too small to use a good structure.

+3
source share

In python, a module combining global data like this is pretty much a singleton. you can do the same with the singleton class or ask yourself if there is any reason why you really need a singleton. If you do not need a singleton - use the usual class (instance). If you need a singleton, google / search SO to find out which template you think is best for you. Maybe you need a module - there is a niche for global - Otherwise, Guido would have cut it out of the language a long time ago - this niche just happens to be quite small ...

0
source share

All Articles