Global

The general meanings of global:
1. involving the entire earth; not limited or provincial in
scope; as, global war; global monetary policy.
2. shaped like a globe, sphere or a ball, "a spherical object"; spherical.
3. broad in scope or content; comprehensive. Opposite of
noncomprehensive.
4. (Computers) Accessible and effective throughout an entire
computer program, rather than in only one subroutine; --
used of variables; as, global variable. Opposite of
local.

Local

Originates from the latin word "locus" meaning place. It means pertaining to or of a
particular place, or to a definite region or portion of space; restricted to one place or
region; as, a local custom.

Python 3

Classroom Training Courses

The goal of this website is to provide educational material,
allowing you to learn Python on your own.
Nevertheless, it is faster and more efficient to attend a "real"
Python course in a classroom, with
an experienced trainer. So why not attend one of the live
Python courses in Strasbourg, Paris, Luxembourg, Amsterdam, Zürich / Zurich, Vienna / Wien, London, Berlin, Munich, Hamburg, Frankfurt
or Lake Constance by Bernd Klein, the author of this tutorial?

This way you will get a perfect training up to your needs and it will be extremely cost efficient as well.
Contact us so we can define and find the best course curriculum to meet your needs, and schedule course sessions to be held at your location.

Skilled Python Programmers

You are looking for experienced Python developers or programmers? We can help you, please
contact us.

Quote of the Day:

"At least for the people who send me mail about a new language that theyâre designing, the general advice is: do it to learn about how to write a compiler. " (Dennis Ritchie)

Global, Local and nonlocal Variables

The way Python uses global and local variables is maverick. While in many or most other
programming languages variables are treated as global if not otherwise declared, Python deals with variables the other way around. They are local, if not otherwise declared. The driving reason behind this approach is that global variables are generally bad practice and should be avoided. In most cases where you are tempted to use a global variable, it is better to utilize a parameter for getting a value into a function or return a value to get it out. Like in many other program structures, Python also imposes good programming habit by design.

So when you define variables inside a function definition, they are local to this function by default.
This means that anything you will do to such a variable in the body of the function will have
no effect on other variables outside of the function, even if they have the same name.
This means that the function body is the scope of such a variable, i.e. the enclosing context
where this name with its values is associated.

All variables have the scope of the block, where they are declared and defined in. They
can only be used after the point of their declaration.

Just to make things clear: Variables don't have to be and can't be declared in the way they are
declared in programming languages like Java or C. Variables in Python are implicitly declared
by defining them, i.e.
the first time you assign a value to a variable, this variable is declared and has automatically the
data type of the object which has to be assigned to it. If you have problems understanding this,
please consult our chapter about data types and variables, see links on the left side.

Global and local Variables in Functions

In the following example, we want to demonstrate, how global values can be used
inside the body of a function:

def f():
print(s)
s = "I love Paris in the summer!"
f()

The variable s is defined as the string "I love Paris in the summer!",
before calling the function f().
The body of f() consists solely of the "print(s)" statement.
As there is no local variable s, i.e. no assignment to s, the value from the
global variable s will be used.
So the output will be the string "I love Paris in the summer!".
The question is, what will happen, if we change the value of s inside
of the function f()? Will it affect the global
variable as well? We test this in the following piece of code:

What if we combine the first example with the second one, i.e. we first access s with a print() function, hoping to get the global value, and then assigning a new value to it? Assigning a value to it, means - as we have previously stated - creating a local variable s. So, we would have s both as a global and a local variable in the same scope, i.e. the body of the function.
Python fortunately doesn't allow this ambiguity. So, it will throw an error, as we can see in the following example:

A variable can't be both local and global inside of a function. So Python decides that we want a
local variable due to the assignment to s inside of f(), so the first print statement before the
definition of s throws the error message above. Any variable which is changed or created
inside of a function is local, if it hasn't been declared as a global variable. To tell Python,
that we want to use the global variable, we have to explicitly state this by using the keyword
"global", as can be seen in the following example:

def f():
global s
print(s)
s = "Only in spring, but London is great as well!"
print(s)
s = "I am looking for a course in Paris!"
f()
print(s)

We have solved our problem. There is no ambiguity left. The output of this small script looks like this:

I am looking for a course in Paris!
Only in spring, but London is great as well!
Only in spring, but London is great as well!

Local variables of functions can't be accessed from outside, when the function call has finished:

def f():
s = "I am globally not known"
print(s)
f()
print(s)

Starting this script gives us the following output with an error message:

We can see that the global statement inside of the nested function g does not affect the variable x of the function f, i.e. it keeps its value 42. We can also deduce from this example that after calling f() a variable x exists in the module namespace and has the value 43. This means that the global keyword in nested functions does not affect the namespace of their enclosing namespace! This is consistent to what we have found out in the previous subchapter: A variable defined inside of a function is local unless it is explicitly marked as global. In other words, we can refer a variable name in any enclosing scope, but we can only rebind variable names in the local scope by assigning to it or in the module-global scope by using a global declaration.
We need a way to access variables of other scopes as well. The way to do this are nonlocal definitions, which we will explain in the next chapter.

nonlocal Variables

Python3 introduced nonlocal variables as a new kind of variables. nonlocal variables have a lot in common
with global variables. One difference to global variables lies in the fact that it is not possible to change variables from the module scope, i.e. variables which are not defined inside of a function, by using the nonlocal statement. We show this in the two following examples:

def f():
global x
print(x)
x = 3
f()

This program is correct and returns the number 3 as the output.
We will change "global" to "nonlocal" in the following program:

def f():
nonlocal x
print(x)
x = 3
f()

The program, which we have saved as example1.py, cannot be executed anymore now. We get the following error:

File "example1.py", line 2
nonlocal x
SyntaxError: no binding for nonlocal 'x' found

This means that nonlocal bindings can only be used inside of nested functions. A nonlocal variable has to be defined in the enclosing function scope. If the variable is not defined in the enclosing function scope, the variable cannot be defined in the nested scope. This is another difference to the "global" semantics.