0% found this document useful (0 votes)
18 views

Functions Scope Namespace

This document discusses functions, scope, and namespaces in Python. It defines a namespace as a "dictionary" that maps names to values. Scope determines which namespaces are searched to find a name. Python uses the LEGB rule to determine scope, searching locally, in enclosing functions, globally, and finally in built-in namespaces. Functions create a new local namespace and scope. Parameters are local variables, and assigning to a parameter creates a local rather than mutating the parent scope variable. An analogy compares namespaces to a stack of papers used for intermediate calculations.

Uploaded by

Salman Masri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Functions Scope Namespace

This document discusses functions, scope, and namespaces in Python. It defines a namespace as a "dictionary" that maps names to values. Scope determines which namespaces are searched to find a name. Python uses the LEGB rule to determine scope, searching locally, in enclosing functions, globally, and finally in built-in namespaces. Functions create a new local namespace and scope. Parameters are local variables, and assigning to a parameter creates a local rather than mutating the parent scope variable. An analogy compares namespaces to a stack of papers used for intermediate calculations.

Uploaded by

Salman Masri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Functions_Scope_Namespace

Ibrahim Abou-Faycal

#
EECE-231
#
Introduction to Computation & Programming with Applications
#
Functions - Scope & Namespace

Reading: [Guttag, Sections 4.1,4.2, 4.5, and 5.3] Material in these slides is based on
• Slides of EECE-230C & EECE-230 MSFEA, AUB
• [Guttag, Chapter 4]
• [MIT OpenCourseWare, 6.0001, Lecture 4, Fall 2016]

0.1 Namespace
• A namespace is kind of a “dictionary”
– It maps names (as strings) to values
– Executing the statement x = 3, mutates a namespace
– When executing the statement print(x), Python looks through a list of namespaces to
try and find one with the name x
– Actually it is technically precisely a dictionary; more on this later

0.2 Scope
• A scope defines which namespaces will be looked in and in what order
• The scope of any reference always starts in the local namespace, and moves outwards
until it reaches the module’s global namespace, before moving on to the builtins, which is
the end of the line
• A concise rule for Python Scope resolution: LEGB Rule
– Local — Names assigned in any way within a function (and not declared global in that
function)
– Enclosing-function — Names assigned in the local scope of any and all statically enclosing
functions (from inner to outer)

1
– Global (module) — Names assigned at the top-level of a module file (or by executing a
global statement in a def within the file)
– Built-in (Python) — Names preassigned in the built-in names module: open, range,
SyntaxError, etc

0.3 Return
• The return statement stops the execution of a function and assigns the returned value
• return is different from print, which only displays values

0.4 What do functions do?


• Whenever a function is defined a new scope and a new namespace are created
– The namespace is the new, local hash of names
– The scope is the implied chain of namespaces that starts at the new namespace, then
works its way through any outer namespaces, up to the global namespace, and on to the
builtins
• Formal parameters are local variables of the function: they exist only in its namespace
• At function call, the actual parameters are assigned to the formal parameters (by
the assignment operator)
• When variables are in scopes other than the local function’s variables they can be accessed,
but can’t be mutated to new parameters (without further syntax)
– Instead, assignment will create a new local variable instead of affecting the variable in
the parent scope
– Best explained with a homework analogy

Stack of namespaces, analogy with working on a homework


• You are working on a math problem in a homework
• You are writing your solutions on Paper A, which you are going to submit
• As an intermediate step, you need to do some lengthy calculations which you don’t have to
submit
• You put on top of Paper A a scratch paper called Paper B to do the calculations
• It turns out that an intermediate step of the calculation on Paper B requires sub-calculations
which you don’t want to write on Paper B to avoid loosing track of the big chain of steps in
Paper B
• You put on top of Paper B another scratch paper called Paper C to do the sub-calculations
• Now you have a stack of 3 papers with Paper C on top, above Paper B which is above Paper
A
• When done with the sub-calculations on Papers C, you write your answer on Paper B, you
put Paper C in the garbage bin, and you go back to the steps on Paper B
• When done with the calculations on Papers B, you write your answer on Paper A, you put
Paper B in the garbage, and you go back to the steps on Paper A
• Think about each paper as namespace

Scopes, analogy with working on a homework


• Say that you have a variable called x on Paper A
• Does it make sense to define a variable called x on Paper B ?

2
• Say that instead of defining x on Paper B, you simply read the value of x while working on
Paper B
• Does this make sense?
−→ YES: have read-only access
• Does it make sense to modify it later on?
−→ NO: too confusing

Tracking maxVal on code visualizer


[ ]: def maxVal(x,y):
if x > y:
return x
else:
return y

v = maxVal(3,7)
print(v)

http://www.pythontutor.com/visualize.html#mode=display

The max of 3 numbers


[ ]: def maxVal3(x,y,z):
a = maxVal(x,y) # assuming the the maxVal has been previously defined
b = maxVal(a,z)
return b

# Calling the function


v = maxVal3(3,10,7)

Visualize it
Get a stack of namespaces of: * depth 1: before calling maxVal3 * depth 2: right after calling
maxVal3 * depth 3: when maxVal3 calls maxVal on x and y * depth 2: when maxVal3 returns *
depth 3: when maxVal3 calls maxVal on a and z * depth 2: when maxVal returns * depth 1: when
maxVal3 returns
Note that x in maxVal3 and x in maxVal are not the same

3
4

You might also like