Navigating Namespaces and Scope in Python (Overview)
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
00:00 Welcome to the course on namespaces and scope in Python. In this course, you will learn what namespaces are and what kind of types of namespaces are used by the Python interpreter.
00:13
You will also learn about variable scope and the LEGB rule. Finally, you will learn about modifying variables out of scope using the global
and nonlocal
keywords and some best practices that come with them.
00:32 The code samples were tested using Python 3.9.0, and as far as I’m aware, all examples should work in older versions of Python as well. The examples also use the default CPython interpreter.
00:48 I noticed some minor differences when I used bpython, so you might also encounter those when you choose to use another interpreter. That’s enough for this introduction, so let’s get started with the first lesson on Python namespaces.
Become a Member to join the conversation.