What I understand from reading the documentation is that Python has a separate namespace for functions, and if I want to use a global variable in that function, I need to use global
.
I'm using Python 2.7 and I tried this little test
>>> sub = ['0', '0', '0', '0']
>>> def getJoin():
... return '.'.join(sub)
...
>>> getJoin()
'0.0.0.0'
It seems things are working fine even without global
. I was able to access global variable without any problem.
Am I missing anything? Also, following is from Python documentation:
Names listed in a global statement must not be defined as formal parameters or in a for loop control target, class definition, function definition, or import statement.
While formal parameters and class definition make sense to me, I'm not able to understand the restriction on for loop control target and function definition.
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:
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 thedoit()
function. You may be more surprised to discover that the code above won't run. The statementprint value
inside the function produces anUnboundLocalError.
The reason is that Python has noticed that, elsewhere in the function, you assign the name
value
, and alsovalue
is nowhere declaredglobal
. 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.It means that you should not do the following:
Global makes the variable "Global"
This makes 'x' act like a normal variable outside the function. If you took the global out then it would give an error since it cannot print a variable inside a function.
While you can access global variables without the
global
keyword, if you want to modify them you have to use theglobal
keyword. For example:In your case, you're just accessing the list
sub
.Any variable declared outside of a function is assumed to be global, it's only when declaring them from inside of functions (except constructors) that you must specify that the variable be global.