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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Navigating Namespaces and Scope in Python (Summary)

Virtually everything that a Python program uses or acts on is an object. Even a short program will create many different objects. In a more complex program, they’ll probably number in the thousands. Python has to keep track of all these objects and their names, and it does so with namespaces.

In this course, you learned:

  • What the different namespaces are in Python
  • When Python creates a new namespace
  • What structure Python uses to implement namespaces
  • How namespaces define scope in a Python program
  • How the LEGB rule applies to scope


Sample Code (.zip)

2.0 KB


Course Slides (.pdf)

414.4 KB

Alain Rouleau on March 3, 2021

Great explanation of namespaces and the concept of scope, thanks!

I’ve seen many people try to explain these Python concepts over the years. But this is one of the best tutorials that I’ve seen. Kudos! I like how you used simple functions to explain things, introduced the globals() function, and nonlocal for enclosed functions as well. Not many people know about those.

Plus, use of the equal sign in f-strings was a nice touch. Definitely going to use that in the future. Not to mention, the "i" command line option. Pretty sure 99% of people don’t know about that feature either which allows you to start an interactive session and import a module ALL in one step. Very handy.

Keep up the good work!

RobyB on March 4, 2021

I agree with you Alan :)

Dirk on March 22, 2021

Short and sweet. I liked it a lot.

Become a Member to join the conversation.