Day 14 of #100DaysOfCode in Python: Navigating Through Scope and Namespaces

Elshad Karimov
3 min readOct 8, 2023

Greetings on Day 14! Today, we dive into a pivotal concept — understanding scope and namespaces in Python. Mastering this aspect is essential to ensure that your variables, functions, and objects exist and operate in the right context, making your code not just functional but efficient and clean.

Understanding Namespaces

A namespace is a container that holds identifiers (names of variables, functions, classes, etc.) and their corresponding objects. Every time you create a variable or function, Python stores it in a namespace, so it knows where to look for it when it’s called upon.

Types of Namespaces:

  1. Built-in Namespace: Contains built-in functions and exceptions. Created when the Python interpreter starts up.
  2. Global Namespace: Contains global variables and functions. Created when the script is executed.
  3. Local Namespace: Contains local variables. Created when a function is called.

Understanding Scope

Scope refers to the region of the code where a namespace is accessible. Python has different scopes for built-in, global, and local namespaces.

Types of Scopes:

  1. Local Scope: A variable defined within a function has a local scope. It’s accessible only within that function, not outside it.
  2. Enclosing Scope: It encloses the local scope and can access variables from the local scope, but not vice versa.
  3. Global Scope: Variables defined at the top-level of a script have a global scope, making them accessible throughout the code.
  4. Built-in Scope: It’s the widest scope containing built-in names that Python provides.

The LEGB Rule:

Python follows the LEGB rule for name resolution:

  • L (Local): Defined inside the function.
  • E (Enclosing): Defined in the enclosing function.
  • G (Global): Defined at the top-level of the script.
  • B (Built-in): Defined in the built-in scope.

Practical Illustration

Let’s take an example:

x = 10  # Global variable

def outer_function():
y = 20 # Enclosing variable

def inner_function():
z = 30 # Local variable
print(x, y, z) # Accessible as per LEGB rule

inner_function()

outer_function()

In this script, x is a global variable, y is an enclosing variable because it's in the scope of outer_function, and z is a local variable within the inner_function. The print statement accesses these variables according to the LEGB rule.

Working with Global and Local Variables

The global Keyword

If you need to modify a global variable from within a function, use the global keyword.

x = 10

def modify_global():
global x
x = 20

modify_global()
print(x) # Outputs: 20

The nonlocal Keyword

The nonlocal keyword is used to work with variables in the nearest enclosing scope that is not global.

def outer_function():
x = 10

def inner_function():
nonlocal x
x = 20

inner_function()
print(x) # Outputs: 20

outer_function()

Challenges for Day 14

  1. Experiment with Scopes: Create a script with global, local, and enclosed variables, and try accessing them in different scopes.
  2. Modify Variables: Create a global variable, modify it inside a function using the global keyword, and print both the original and modified values.
  3. Nested Functions: Create a function within a function and try accessing the outer function’s variables from the inner function.

In Conclusion

Understanding scope and namespaces is akin to mastering the rules that govern the existence and accessibility of variables and functions in your Python universe. It ensures your code remains organized, variables don’t clash, and functions operate seamlessly. Every day, you’re inching closer to becoming a Python wizard. Keep exploring the cosmos of code! 🌌🔍 #PythonScope

--

--

Elshad Karimov
Elshad Karimov

Written by Elshad Karimov

Software Engineer, Udemy Instructor and Book Author, Founder at AppMillers

No responses yet