Python handles globals badly.

Ian Kelly ian.g.kelly at gmail.com
Thu Sep 3 21:47:32 CEST 2015


On Thu, Sep 3, 2015 at 1:05 PM,  <tdev at freenet.de> wrote:
>     If this would be under the developer responsibility than this
>     is simply achieved by giving well-written var names.

So, adopt a rule whereby you prefix all your global variable names
with "global" or "g_"? How is this superior to just declaring them
once per function that needs to set them? You're just shifting the
extra typing from one place to another.

>     Or does anyone really name a global var xxx and a function var xxx?
>     I am sure no one at all will do it. I dont want read such a code.

Intentionally, it's probably rare. But if I'm adding a new variable, I
shouldn't need to first make sure that it's safe to do so by scanning
over the entire file to make sure that the name hasn't already been
used elsewhere in the opposite scope.

>  Function calls (?) -
>    I have to admit I did not really get the problematic from
>    the sample provided by Chris Angelico.
>
>    What I can see or mean to see is:
>        it has nothing to do with global-keyword from the high level point of
>        view: give write access, probably to the vars word and otherword.
>
>        And yes, I see the vars independant.
>        And cause you set no "global" they are simple local vars
>        (readable+writeable)

So in order for something to be global, it would have to be referenced
at least once in the global scope? Currently it's possible to do this:

def set_foo(value):
    global foo
    foo = value

def get_foo():
    return foo

With your proposal that would change to:

foo = None

def set_foo(value):
    foo = value

def get_foo():
    return foo

So again the declaration has just been moved from one place to another.

> But then I ask you from high-level point of view
> (if my high level view is correct at all):
> Would you remove this keyword if it would be technically possible
> or is good for you from high level point of view to have a keyword "global"?

Provided that the proposal doesn't open up the possibility of
unintentionally creating a global variable where I wanted a local, I'd
be okay with it.


More information about the Python-list mailing list