Navigating Namespaces and Scope in Python

In a program of any complexity, you’ll create hundreds or thousands of names, each pointing to a specific object. How does Python keep track of all these names so that they don’t interfere with one another? This course covers Python namespaces, the structures used to organize the symbolic names assigned to objects in a Python program.

In this course, you’ll learn:

  • How Python organizes symbolic names and objects in namespaces
  • When Python creates a new namespace
  • How namespaces are implemented
  • How variable scope determines symbolic name visibility
  • What is the LEGB rule

What’s Included:

Downloadable Resources:

Related Learning Paths:

About Johan Vergeer

Johan Vergeer Johan Vergeer

Hallo, my name is Johan, and I am a software engineer working for the Pythoneers at Ordina in the Netherlands. I have a soft spot for awesome software design, clean code and sharing knowledge.

» More about Johan

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Participant Comments

Dirk on March 22, 2021

Short and sweet. I liked it a lot.

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!

« Browse All Courses