PyMyth: Global variables are evil... WRONG!

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Nov 13 21:45:16 EST 2013


On Wed, 13 Nov 2013 18:10:59 -0800, Rick Johnson wrote:

> On Wednesday, November 13, 2013 5:42:24 PM UTC-6, Rhodri James wrote:
>> On Tue, 12 Nov 2013 02:06:09 -0000, Rick Johnson wrote:
>> > PyMyth: Global variables are evil... WRONG!
>> That's not a PyMyth.  It's a CompSciMyth, or to be more accurate a good
>> general Software Engineering guideline regardless of language.  Like
>> all guidelines it can be broken, but people who break it should do so
>> knowingly, aware that they have created potential problems for
>> themselves.
> 
> You speak as if using globals are inhabited by gremlins just wanting to
> get out and run a muck. There are no "inherent" problems in the global
> interface design except those that programmer inserts himself.
> 
> Real global variable interfacing is no different than python object
> member interfacing, or module interfacing, except that the latter two
> are encapsulated by inside a namespace, and the former (historically)
> are unprotected and available everywhere; hence the name "global" :-)

A fully-auto machine gun with a hair-trigger and no safety is no 
different from a single-barrel shotgun with a safety and a trigger lock! 
You can blow your foot off with both!

Yes, but in the first case you can do it by accident, while with the 
second you have to work hard to blow your foot off.


> So that renders this whole argument of "global gremlins" as FUD. The
> gremlins don't exist until the programmer creates them.

Wait... are you saying that software bugs only occur because the coder 
writes buggy software? I thought bugs came into existence via spontaneous 
generation, like viruses, bacteria, pigeons and Marine Corps sergeants.


> And i also want to point out the hypocrisy of Python's design. Python
> DOES have globals

Is that why it has a global keyword and a globals() function? Thanks for 
the information, I never would have worked that out without your keen 
insight.


-- 
Steven



More information about the Python-list mailing list