Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Enclosing and Local Namespaces

00:00 Next to the globals() function, Python also provides a corresponding built-in function called locals(). It’s similar to globals(), but accesses objects in the local namespace instead.

00:15 The interpreter creates a new namespace whenever a function executes, and that namespace is local to the function and remains in existence until the function terminates.

00:28 If you would like to know more about how the local namespace behaves, then you could use a function like this. It starts with printing where the function starts.

00:42 Next, it declares a variable called s, after which it declares an enclosed function called g(), which again prints where it starts, declares a variable, and prints where it ends. Finally, within the enclosing function f(), you call the function g(). And finally, you print where the function f() ends. So now if you want to know what the local namespace contains, you can do this by printing the return value of the locals() function. In this case, let’s see what the local namespace contains after the function f() started and a variable s is declared.

01:39 In the Python REPL, you load your file, which I called local_namespaces.py, and you run the function f() with a couple of parameters.

01:52 Now you see that at this point in the function, the local namespace contains both parameters and the variable s.

02:06 Now let’s see what the local namespace will contain after the enclosed function g() is called. So let’s move this call to the locals() function to the end of the function f() and run it again.

02:28 Now you see that the local namespace of the function f() still contains both parameters, still contains the variable s, and the function g() is added.

02:45 But what happens within the enclosed function g()? Well, in order to figure that out, we move the calls to the locals() function inside the enclosed function g() and run it again!

03:02 Now you see that at this moment, the local namespace of the function g() only contains the variable z.

03:12 A nice thing about local namespaces is that you also have access to the local namespace of the enclosing function within the enclosed function. So when you would like to print the value of s within the function g(), you can do that because the function g() has access to all names within the namespace of its enclosing function. So, again, you run the module and you call the function f().

03:50 And now you see that you were able to access s, and s is also copied to the namespace of the enclosed function g(). And that brings us to the end of this section about the types of namespaces in Python. In the next section of this course, you will learn about variable scope.

Become a Member to join the conversation.