Local and Global Names
Consider the following code:
x = 5
def f():
x = 9
print "Within f: x =", x
print "Before f: x =", x
f()
print "After f: x =", x
-
Try to figure out what gets printed,
then run it to see if you're right.
-
Does it make sense?
Re-run under the debugger and examine variables.
-
Scope:
Python's mechanism to associate names with values.
-
Function parameters and variables assigned a value
within a function are local variables:
new variables that exist only while the function is executing,
and are accessible only from within the function.
-
This makes it possible to reuse common variable names between
functions, with no unpredictable side-effects— each name refers
to a distinct variable local to its function only.
-
Local variables can have the same name as global variables
(those declared outside of any function).
In that case, the global variable is inaccessible from within the
function.
-
In our example, the
x
within the body of f
is
a local variable that overshadows the global variable
x
— i.e., it prevents the global variable
from being accessible from within f
.
-
But it gets a little stranger...
x = 5
def f():
print "Within f: x =", x
x = 9
print "Within f: x =", x
print "Before f: x =", x
f()
print "After f: x =", x
-
This fails because Python scans the entire code once before starting to
execute it, and realizes that
x
is a local variable in
f
. Because this happens before f
is called,
the first line refers to the local x
, which has yet to be
assigned a value...
-
What if we want to access the value of a global variable in a
function?
-
Just don't create any local variable (including function parameters)
with the same name. For example:
x = 5
def f():
print "Within f: x =", x
y = 9
print "Within f: y =", y
print "Before f: x =", x
f()
print "After f: x =", x
# print "After f: y =", y # generates an exception
-
What if we want to change the value of a global
variable inside a function?
-
Just using a simple assignment won't work, as demonstrated above.
-
Need to let Python know that, within the function, the name we care
about is a global variable and should not be redefined locally.
x = 5
def f():
global x
print "Within f: x =", x
x = 9
print "Within f: x =", x
print "Before f: x =", x
f()
print "After f: x =", x
Namespaces
-
A namespace keeps track of known names
within a certain portion of the code
-
Namespaces are created when we begin in the Python shell,
or in the main block of a file
-
A new namespace is created whenever a function is called
(not when it is defined)
-
A function's namespace is destroyed when a call terminates
How Python looks up a name
-
Start in the "local" namespace—
the one the currently executing statement is a part of
-
If the name is not found, look "up" in the enclosing namespace—
generally, this is the "global" namespace
(the main block of a file or the Python shell)
-
There can be other enclosing namespaces when using
nested functions or classes
(but we won't use nested functions in this course, and
we'll discuss classes toward the end of the course only)
-
If the name is not found in the local or global namespaces
(or in enclosing namespaces in between),
look in the "builtin" namespace
-
If the name is still not found, it is unknown—
this is an error