Python handles globals badly.
rosuav at gmail.com
Sat Sep 5 03:54:30 CEST 2015
On Sat, Sep 5, 2015 at 11:42 AM, Michael Torrie <torriem at gmail.com> wrote:
> 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
Yeah. Comes to the same thing; if you use a dotted lookup, it's not
assigning to the global.
Of course, you still have all the other concerns about globals. You've
just buried them behind a level of indirection.
> 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.
Indeed. The key to being a good programmer is not "write your code
despite the language you're using", but "write the code in the
language you're using".
More information about the Python-list