osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How should we use global variables correctly?


On 8/22/19 12:00 PM, Windson Yang wrote:
> I can 'feel' that global variables are evil. I also read lots of articles
> proves that (http://wiki.c2.com/?GlobalVariablesAreBad). However, I found
> CPython Lib use quite a lot of `global` keyword. So how should we use
> `global` keyword correctly? IIUC, it's fine that we use `global` keyword
> inside the Lib since most of the time the user just import the lib and call
> the API. In any other situation, we should avoid using it. Am I right?

It should be noted that the Python 'global' is much more like the C/C++
file scoped 'static' which is less bad that fully global variables (the
extern variable).


-- 
Richard Damon