Python handles globals badly.

Michael Torrie torriem at gmail.com
Fri Sep 4 21:42:21 EDT 2015


On 09/04/2015 06:27 PM, Chris Angelico wrote:
> If you want the first one, well, there are languages like that, and
> you're welcome to use those. For the latter, it's easy enough to do
> something like this:
> 
> import types
> _g = types.SimpleNamespace()
> 
> def accumulate(x):
>     _g.accum += x
>     return _g.accum
> 
> Look, Ma! No global statement!

Since most of the time for me when I need a global, I need it to be an
app global (more than just one module) and I use it to store
configuration.  So I just use another module for that.

import my_global_module as _g

_g.some_setting = 5

I get the impression, thought, that our esteemed poster is still trying
to battle the Java windmill, but in Python now and will never accept
anything we try to tell him about the Python way.  Python does have its
warts, but often attempts to fix the warts would just make things a lot
worse.  So I accept them as part of Python's character and try to use
them to my advantage.



More information about the Python-list mailing list