scope.py
#----------------------------------------------------------------------
# This code demonstrates the variable scope rules in Python
#----------------------------------------------------------------------
#----GLOBAL VARIABLES-------------------------------------------------
# first I will define a "global" variable, A. A global variable is not
# defined within any function. It is defined at the top level (no
# indentation). Global variables (a.k.a. "globals") are accessible from
# all parts of the program hence they are global in scope.
A=1
print "global A =",A
# Now I define a function that accesses A to make sure it is visible from
# inside a funtion
def function1():
print "function1: I can see A:",A
return
# Call the function to see if it works
function1()
#----LOCAL VARIABLES-----------------------------------------------------
# Now I define a function wich has a "local" variable, B. A local
# variable is defined within the body of a function and is only visible
# to code within the body of that function. A local varable is
# therefore said to have "local scope". I can't access local
# variables (a.k.a. "locals") from outside the function it is defined
# in and that variable no longer exists as soon as the function
# returns. A new local will be created every time the function is
# called and destroyed with the function returns. When the functions
# returns we say the local variables are "out of scope".
def function2():
B=2 # a local variable is created
print "function2: local B =",B
B=3 # I can assign a new value to it
print "function2: local B changed to:",B
return # here the local is destroyed forever
function2() # call the function and see what happens
# The following print statement is not in the body of function2 so it can't
# "see" the B variable. If you uncomment this statement it will give an
# error saying: "NameError: name 'B' is not defined"
# print B
# If I use the same name for a local variable as I (or some person
# using my function in their program) used for some global variable
# then the local variable supersedes the global variable. Assigning a
# new value to the local doesn't affect the global with the same
# name. The global variable is effectively hidden behind the
# local. The global then becomes inaccessible to code within the
# function. They are two different variables which have the same name
# but different scope: global versus local.
A = 1 # global
#define a function which has a local also called 'A'
def function4():
A=2
print "function4: local A =",A
return
function4()
print "global A is still",A
# Life would be very difficult for programmers if local variables did
# not have this behavior of blotting out globals with the same name
# because if they did not, then anytime I used a function whose author
# had chosen the same name for one of their locals as I had used in my
# program, my variable would magically seem to get changed everytime I
# called that function. I would have to know all of the names of all
# of the locals in all of the functions I was planning to use so I
# could be sure not to use any of the same variable names. If someone
# used 't' for time and 'x' for something else in their function, I
# couldn't reliably use those variable names. So we should be thankful
# for local scope!
#-----PARAMETERS-------------------------------------------------------
# "Parameters" are like local variables but they represent values that
# will be passed to the function in its argument list when it is
# called. The following function has a single parameter named
# "param". Parameters are the main way functions get information from
# the outside world. Technically functions can already "see" the
# variables defined "outside" of the function definition but it is
# generally bad practice to assume that globals will exist and have
# the proper values at the time the function is called. It would
# also make life more difficult for users of this function because
# they have to know about all globals that must be defined because the
# function expects them. Users couldn't use those reserved variable
# names for anything else. Its better to pass all "inputs"
# the function might need to do its work in as parameters to the
# function.
def function3(param):
print "function3: parameter 'param' =",param
return
# When I call this function, the values I place in the
# parentheses are called arguments. Arguments are values that get
# assigned to parameters each time the function is called and its body
# is executed.
# try calling function3 with different "arguments" passed into the
# function's paramter.
function3(4)
function3("Dude!")
# Similar to local variables, parameters may also have the same name
# as global variables without any conflict.
def function5(A):
print "function5: parameter A =",A
return
function5(5)
function5("Yo!")
print "Global A is still",A
#---NESTED SCOPE-------------------------------------------
# NOTE: The following describes a finer point of the variable scoping rules
# that we will rarely encounter especially in well written code where
# functions don't depend on the values of variables defined externall
# in the enclosing scope. I only include this information to be
# complete, not because I expect everyone to memorize all of these
# finer points.
#
# It is possible to have multiple layers of variable scope nested
# within one another. The inner scope can always see the variables dfined
A=1 # A is defined in global scope
def function7():
print "function7: A =",A # function6 can still sees global A
return
def function6():
A=2 # local A hides global A within function 6
print "function6: local A =",A
function7() # call function6 from within function6
return
# When function7 calls function6, function6's scope is nested in
# function7's scope so now will function6 see the local A defined in
# function7 or the global A when called from within function7?
function6()
# function7 sees global A even though function 6, the calling
# function, has defined a local called A, but the global A takes
# precedence. The call to function7 nested inside function6 still sees
# the global A, not the A from the enclosing function's scope.
Generated by GNU enscript 1.6.3.