Pylint - "Using a Global Statement"

I am trying to work on my current python program, very aware of pylint warnings, so although I understand that I can just turn off the warning, I want to be sure that there was no more desirable solution before.

My current section of code is my registration code. I have an init() function that installs handlers and attaches them to the root log, and the set_console_level(level) function sets the level at which console messages are reported (DEBUG, INFO, etc.):

 _CONSOLE_OUT = NONE def init(): """Set up the error handlers for the root logger""" console_out = logging.StreamHandler() console_out.setLevel(logging.INFO) #Show info messages for now #... logging.root.addHandler(console_out) global _CONSOLE_OUT _CONSOLE_OUT = console_out #Rest of function sets up more handlers similarly init() def set_console_level(level): """Change the level at which console messages are printed""" assert __CONSOLE_OUT is not None #Set in init _CONSOLE_OUT.setLevel(level) 

As far as I can tell from my reading, this is a way to do it. The global _CONSOLE_OUT required to write to the module level variable. However, when I run pylint in this file, I get a warning W: 15,4:init: Using the global statement . Is there any way to avoid this warning? Best way to approach this problem?

(Please do not say global _CONSOLE_OUT #pylint: disable=W**** without specification)

+4
source share
3 answers

You can create it during import:

 _console_out = ConsoleHandler() 

where ConsoleHandler is a factory function or your class that creates and sets up a handler.

+2
source

This is a weird template for setting items outside the method area. This will lead to problems later because you will not know where the global variables are set.

Your code also relies on the execution order of methods to set variables that are supposedly dependent on other parts of your code. This can cause problems later if you forget to call init() , especially when importing things (of course, you can always insert it into __init__.py , but it is not).

Given them, I think another way is to create a class and set a variable inside the class (giving it a different namespace); or if you want to keep it as it is, since Sebastian suggested returning the configuration variables.

You can return the tuple and then unzip it to create your own global variables if you need to set more than one.

 >>> def foo(): ... return "hello","world" ... >>> a,b = foo() 
+1
source

One thing I thought about looking at the code (and with Burhan Khalid's answer) is that there is no reason why my init() code should be in a function at all, since I just want it to run once when imported. I believe that the only reason I initially did this was because the handler variables would not be modular.

So, instead, my code might just be

 _CONSOLE_OUT = logging.StreamHandler() _CONSOLE_OUT.setLevel(logging.INFO) #Show info messages for now #... logging.root.addHandler(_CONSOLE_OUT) #... def set_console_level(level): """Change the level at which console messages are printed""" assert _CONSOLE_OUT is not None #Set at root _CONSOLE_OUT.setLevel(level) 
0
source

All Articles