Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scoping and Namespaces CSIS 1595: Fundamentals of Programming and Problem Solving 1.

Similar presentations


Presentation on theme: "Scoping and Namespaces CSIS 1595: Fundamentals of Programming and Problem Solving 1."— Presentation transcript:

1 Scoping and Namespaces CSIS 1595: Fundamentals of Programming and Problem Solving 1

2 Namespaces Namespace: – List of all variables and their current values – Maintained by Python environment Example: x8 y15 z“hello”

3 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

4 Namespace Example Namespace for main program x 2 Namespace for scope_demo x 1

5 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

6 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

7 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 50 c 10 Namespace for main program f 50 c 10 Namespace for fahrenheit2celsius f 50 c 10 Namespace for main program x 50 y 10

8 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!

9 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

10 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 2 x 2 scope_demo x 1 main program x 2 scope_demo x 1 main program x 2

11 Stack for Population Program Main program get_ years 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

12 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)

13 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


Download ppt "Scoping and Namespaces CSIS 1595: Fundamentals of Programming and Problem Solving 1."

Similar presentations


Ads by Google