PyMyth: Global variables are evil... WRONG!

Rick Johnson rantingrickjohnson at gmail.com
Wed Nov 13 21:10:59 EST 2013


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" :-)

With Python, i can still reassign a value to class attribute
and break code that depends on that attribute, of course,
doing so is not a good idea; but it's allowed nonetheless!

The programmer can break ANY interface in Python, be it
accessing a module attribute he shouldn't touch or via a
"real global" variable (if Python had that functionality).

So that renders this whole argument of "global gremlins" as
FUD. The gremlins don't exist until the programmer creates
them. Just because one programmer is unable to create a
logical global implementation, does not mean another cannot.

All you need to do is adopt the principles of consistency.

But i'm not here to convince you that globals are correct
for you, nor do i want you to use them if you feel
uncomfortable (lord knows there's enough bad code
circulating already!) i just want people to stop propagating
this myth that globals are evil.

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





More information about the Python-list mailing list