Skip to content

Latest commit

 

History

History
236 lines (153 loc) · 4.38 KB

README.md

File metadata and controls

236 lines (153 loc) · 4.38 KB

Chapter 10: Scope and Namespace

Table of Contents

Understanding Scope in Python

Scope refers to the region of the code where a variable is accessible. In Python, there are different types of scopes: local, enclosing, global, and built-in. Understanding scope is crucial for writing clean and bug-free code.

Example: Local Scope

Variables defined inside a function are in the local scope and can only be accessed within that function.

def my_function():
    x = 10  # Local scope
    print(x)

my_function()
print(x)  # This will raise an error because x is not defined outside the function

Output:

10
NameError: name 'x' is not defined

Explanation: The variable x is defined inside the function my_function and is not accessible outside the function.

The LEGB Rule (Local, Enclosing, Global, Built-in)

The LEGB rule defines the order in which Python looks for variables. It stands for Local, Enclosing, Global, and Built-in.

Local Scope

Variables defined inside a function.

def my_function():
    x = 10  # Local scope
    print(x)

my_function()

Output:

10

Enclosing Scope

Variables in the local scope of enclosing functions.

def outer_function():
    x = 20  # Enclosing scope

    def inner_function():
        print(x)

    inner_function()

outer_function()

Output:

20

Global Scope

Variables defined at the top level of a script or module.

x = 30  # Global scope

def my_function():
    print(x)

my_function()
print(x)

Output:

30
30

Built-in Scope

Names that are preassigned in Python.

print(len("Hello"))  # Built-in function

Output:

5

Global and Nonlocal Keywords

The global and nonlocal keywords are used to modify the behavior of variables in different scopes.

Global Keyword

The global keyword allows you to modify a global variable inside a function.

x = 40

def my_function():
    global x
    x = 50

my_function()
print(x)

Output:

50

Explanation: The global keyword allows the function to modify the global variable x.

Nonlocal Keyword

The nonlocal keyword allows you to modify a variable in the enclosing scope.

def outer_function():
    x = 60

    def inner_function():
        nonlocal x
        x = 70

    inner_function()
    print(x)

outer_function()

Output:

70

Explanation: The nonlocal keyword allows the inner function to modify the variable x in the enclosing scope.

Namespace and Variable Resolution

A namespace is a container that holds a set of identifiers (variable names) and their corresponding objects. Python uses namespaces to keep track of variables and their values.

Example: Namespace

x = 80  # Global namespace

def my_function():
    x = 90  # Local namespace
    print(x)

my_function()
print(x)

Output:

90
80

Explanation: The variable x in the global namespace is different from the variable x in the local namespace of the function.

Variable Resolution

Python resolves variables using the LEGB rule.

x = 100  # Global scope

def outer_function():
    x = 110  # Enclosing scope

    def inner_function():
        x = 120  # Local scope
        print(x)

    inner_function()

outer_function()

Output:

120

Explanation: The variable x in the local scope of inner_function is used, as it is the closest scope according to the LEGB rule.

Summary

In this chapter, we covered scope and namespace in Python, including the LEGB rule, the global and nonlocal keywords, and how Python resolves variables.

Tasks

  1. Write a program that demonstrates the use of local, enclosing, and global scopes.
  2. Write a program that uses the global keyword to modify a global variable inside a function.
  3. Write a program that uses the nonlocal keyword to modify a variable in the enclosing scope.
  4. Write a program that demonstrates variable resolution using the LEGB rule.

Next Chapter: Conclusion and Further Learning