scoping and namespaces csis 1595: fundamentals of programming and problem solving 1
TRANSCRIPT
Scoping and Namespaces
CSIS 1595: Fundamentals of Programming and Problem Solving 1
Namespaces
• Namespace: – List of all variables and their current values– Maintained by Python environment
• Example:
x 8
y 15
z “hello”
Namespaces and Functions
• Each function has different namespace• Variable in function has no effect on same variable in
any other function– Like two “separate universes”
Spock in main program
Spock in separate function
Namespace Example
Namespace for main program
x 2Namespace for scope_demo
x 1
Arguments and Namespaces
• Arguments to functions are also local to a namespace– Example: Fahrenheit to Celsius program– Note that f and c are different in function, main program
Namespace for fahrenheit2celsius
f
c
Namespace for main program
f
c
Arguments and Namespaces
• Key idea: Does not matter whether same or different variable names used in functions– Will be treated as different variables regardless
• Example: Two versions of fahreheit2celsius program– Both do the same thing
Passing Arguments by Value
• Key idea:– Variables not passed or returned– Only their values are passed/returned
– In both cases the value of 50 passed and the value of 10 returned
Namespace for fahrenheit2celsius
f 50c 10
Namespace for main program
f 50c 10
Namespace for fahrenheit2celsius
f 50c 10
Namespace for main program
x 50y 10
Namespaces and Functions
• Why is this important?– Same function may be called from multiple places
with different arguments• Example: “Largest of 4 numbers” program
– Programs often created by hundreds of programmers• Each creates set of functions• Should not have to worry about what variables other
programmers are using!
– Functions often put in separate files (modules)• No main program associated with it!
Functions Calling Functions
• Separate namespace created for each call
Namespace for main program
x 2
Namespace for scope_demo
x 1
Namespace for scope_demo2
x 7
Activation Stack• Each current namespace = “activation record”• Maintained on “stack” of activation records– When function called, its namespace pushed on top of stack– When return from function, that namespace removed– Return to function immediately below it on stack
main program
x 2
scope_demo
x 1
scope_demo2
x 7
main program
x 2main program
x 2
scope_demo
x 1
main program
x 2
scope_demo
x 1
main program
x 2
Stack for Population Program
Main program
Main program
get_years
Main program
Main program
print_table
Main program
print_table
compute_populat
ion
Main program
print_table
Main program
print_table
print_line
Main program
print_table
Main program
print_table
compute_populat
ion
Main program
print_table
Main program
print_table
print_line
Scoping
• Key idea: Namespace only exists while function active• Scope of variable = function it is defined in– Does not exist outside of its scope (error if referenced)
Scoping and Global Variables
• Exception: Functions may refer to variables in main
• Exception to the exception: If value assigned in function it becomes local to function
• Just avoid doing this!
Prints 3
Still 3