Now that we have examined the built-in and global namespaces, let’s dive into the deepest level of namespaces - the local namespace. To do so, let’s start by examining this program:
global_variable = 'global' def add(num1, num2): nested_value = 'Inside Function' print(num1 + num2) add(5, 10)
Here, we have defined two values: a global_variable
and a add()
function. In Python, whenever the interpreter executes a function, it will generate a local namespace for that specific function. This namespace only exists inside of the function and remains in existence until the function terminates.
Similar to how we can see the global namespace using a built-in function called globals()
, Python provides a function called locals()
to see any generated local namespace. Let’s refactor our program just slightly and see what exists inside of a local namespace:
global_variable = 'global' def add(num1, num2): nested_value = 'Inside Function' print(num1 + num2) print(locals()) add(5, 10)
Would output:
15 {'num1': 5, 'num2': 10, 'nested_value': 'Inside Function'}
Notice the following:
We called
locals()
inside theadd()
function to get the local namespace generated when the function is executed. If we calledlocals()
outside of a function in our program, it behaves the same asglobals()
.The value printed from calling
locals()
represents the namespace that only exists inside of the function. Notice even the function parametersnum1
andnum2
exist alongside the variable namenested_value
. The namespace does not includeglobal_variable
since it exists outside of the function (in the global namespace).
Let’s now practice examining the local namespace!
Instructions
Note: Since we will be printing a lot of namespaces, the current editor has markers for where to write each checkpoint.
First, let’s look at the differences between the global and local namespaces for a file with one variable defined. In the code editor, write two print statements that:
- print
locals()
- print
globals()
Examine the output of both. Notice any similarities?
Now that we have seen what a local namespace looks like without any other code, let’s add some functions so we can check out local namespaces inside of a function.
Create a function called divide()
that has two parameters num1
and num2
.
The function should create a variable called result
that is the result of dividing num1
by num2
. The function should then also print locals()
.
Let’s add some more names! Create a function called multiply()
that has two parameters num1
and num2
.
The function should create a variable called product
that is the result of multiplying num1
by num2
. This function should also print locals()
.
To see our local namespace inside divide()
, we need to execute it. Call divide()
with the values of 3
& 4
. Examine the local namespace it generates.
To see our local namespace inside multiply()
, we need to execute it. Call multiply()
with the values of 4
& 50
. Examine the local namespace it generates. Notice any similarities?
Let’s examine how the local namespace called outside of functions has changed. Print locals()
once more. Notice any changes?