I think you are confused with php which requires the use of the global keyword - the python docs confirm this - basicly if it isn't defined in the local context it is treated as global
–
tobyodaviesJan 14 '11 at 16:15

2

Be careful with your wording: Python does not have a seperate namespace for functions (that would mean you could have def foo(): ... and foo = ... at the same time). It does create a new scope for every function call. (But how is this different from about every other remotely high-level language in the world?)
–
delnanJan 14 '11 at 17:00

In addition, due to the nature of python, you could also use global to declare functions, classes or other objects in a local context. Although I would advise against it since it causes nightmares if something goes wrong or needs debugging.

"global" in this context appears to be different from other languages consider "global". In Python a "global" reference is still in the confines of the module and would need to be referenced from outside that module as "module.global_var" rather than simply "global_var" as one might expect.
–
juiceOct 16 '13 at 5:30

4

@juice - In Python there is no such thing as absolute globals automatically defined across all namespaces (thankfully). As you correctly pointed out, a global is bound to a namespace within a module, however it can be imported into another module as from module import variable or import module.variable. In the first case, the import would make the variable accessible as variable without requiring reference as module.. If it's considered a global in the scope of the module will depend on where it's imported. See also nonlocal as a new scope related keyword in python 3.
–
UnodeOct 16 '13 at 17:07

1

Could you explain why global variables are not a good solution? I often hear this, but in my current project they seem to do just what I need them to do. Is there something more sinister I haven't thought of?
–
Robin NewhouseOct 18 '13 at 13:05

1

@RobinNewhouse There's a few questions on SO already covering thattopic and other non SO articles.
–
UnodeOct 18 '13 at 16:52

This is the difference between accessing the name and binding it within a scope.

If you're just looking up a variable to read its value, you've got access to global as well as local scope.

However if you assign to a variable who's name isn't in local scope, you are binding that name into this scope (and if that name also exists as a global, you'll hide that).

If you want to be able to assign to the global name, you need to tell the parser to use the global name rather than bind a new local name - which is what the 'global' keyword does.

Binding anywhere within a block causes the name everywhere in that block to become bound, which can cause some rather odd looking consequences (e.g. UnboundLocalError suddenly appearing in previously working code).

The other answers answer your question. Another important thing to know about names in Python is that they are either local or global on a per-scope basis.

Consider this, for example:

value = 42
def doit():
print value
value = 0
doit()
print value

You can probably guess that the value = 0 statement will be assigning to a local variable and not affect the value of the same variable declared outside the doit() function. You may be more surprised to discover that the code above won't run. The statement print value inside the function produces an UnboundLocalError.

The reason is that Python has noticed that, elsewhere in the function, you define the name value and value is not declared global. That makes it a local variable. But when you try to print it, the local name hasn't been defined yet. Python in this case does not fall back to looking for the name as a global variable, as some other languages do. Essentially, you cannot access a global variable if you have defined a local variable of the same name anywhere in the function.

This tripped me up many times, and I hope to avoid you tripping over the same thing.

Thanks for pointing this out. So, as long as your local scope doesn't assign to a name that exists outside of it, then references in the local scope will use the outside name. However, if anywhere in your function you do assign to that name, references in that local scope, even before the local assignment, do not look outside.
–
dimadimaFeb 21 '13 at 17:54