the global keyword:

Steven D'Aprano steve at
Thu Jun 23 21:00:39 EDT 2016

On Wed, 22 Jun 2016 07:24 pm, BartC wrote:

> But even if it worked, I don't think this two-parter counts as a 'global
> variable' as it is understood. I might as well just create my own G
> module containing:
>      BDFL="GvR"
> and import it in both A and B. Then I can also write G.BDFL in those
> modules. But remember the point was avoid having to write Test.test_var.

The question "Does Python have global variables?" depends on what you mean
by "global variable". That makes it a dispute about definitions, and we
know how they go:

So what do *I* mean by global variables?

To me, a global variable is a variable which is scoped to a level wider than
any single function, i.e. module scope, or whole-application scope. That
is, the variable must be visible to more than one function, or more than
one module.

But further, you must be able to *assign* to that variable with a simple
assignment, without explicitly prefixing the variable with a namespace:

foo = 1

It is okay if you have to declare that the variable is global before
assigning to it.

So to me, Python has module globals, because you can have two functions in
the one module which both assign to the same variable:

def spam():
    global x
    x = 1

def ham():
    global x
    x = 2

x is a global variable. But Python *doesn't* have application-wide globals,
because although you can access a variable across multiple modules at the
same time, you cannot do so without using the fully-qualified name module.x
rather than just x.

(To be precise, for the builtins module specifically, you can *read* the
value of the variable using just x, but you cannot *assign* to it unless
you use the fully-qualified name builtins.x.)

If Rick wishes to argue for a different definition of "global variable",
Rick should explain what his definition is, and why we should prefer it to


More information about the Python-list mailing list