PyMyth: Global variables are evil... WRONG!

Rick Johnson rantingrickjohnson at gmail.com
Wed Nov 13 03:45:31 CET 2013


On Tuesday, November 12, 2013 4:41:34 PM UTC-6, jongiddy wrote:
> On Tuesday, November 12, 2013 5:00:37 PM UTC, Rick Johnson wrote: 
> >    1. Accept that globals are useful, and make them
> >       available through a "real" global syntax, not
> >       some attribute of a module that "appears" to be
> >       local, but in reality is global. Then prevent
> >       external mutation of module attributes directly,
> >       and instead, require that mutation must follow a
> >       contract defined by internal module "setter"
> >       functions.
> >    2. Remain convinced that global mutation is evil
> >       and prevent mutation of all python module
> >       attributes. You can import modules, but you can't
> >       mutate their contents.
> From your first post, I take it you're not keen on option #2.

That is correct, i prefer to use REAL interfaces.

> For #1, module globals are exactly the hierarchically
> namespaced globals that you desire in your first post,

Agreed.

> except they are variables, not get/set handlers (which is
> what I take you to mean by an "interface"). 

Yes, python modules expose ALL members publicly and have no
support for contractual interfacing.

> Why not create a PEP to provide handlers for module
> attributes? You could base it on PEP 213, which describes
> the same feature for classes. 

I would love to see modules expand to something more than
mere "boxes to stuff variables". But then again, modules
are not going to provide the ease of access that globals
demand due to the import problem.

Modules will not be a good place to store globals because
modules must be imported and globals should never need
importing. 

I've just released a module on Python-list called "G.py"
that does exactly what i propose. It's not perfect, but i
think you'll understand what i'm proposing after testing 
it.

> As a bonus, this would trivially support option #2 (e.g.
> non-mutable math.pi) by raising an exception for the set
> operator.

"math.pi" should be "math.PI". and PI should be a CONSTANT. 
And not just a pseudo constant, but a REAL constant that 
cannot be changed.



More information about the Python-list mailing list