Python scope rules with examples

Spark 11 Published: 12/04/2024

Python scope rules with examples

Here are some basic concepts and rules to keep in mind when working with Python scopes:

Local Scope: The local scope refers to the variables defined within a function or block of code. These variables can be accessed anywhere within that function or block, but not outside.

Example:

def my_function():

x = 5

print(x) # prints 5

my_function()

print(x) # Error: x is not defined here

Global Scope: The global scope refers to the variables defined at the top level of a Python program or within a module. Global variables can be accessed from any function or block within that module.

Example:

x = 5

def my_function():

print(x) # prints 5

my_function()

print(x) # prints 5

Enclosing Scope: The enclosing scope refers to the outermost scope that a variable belongs to. This is where we find variables defined in parent functions or modules.

Example:

def outer_function():

x = 10

def inner_function():

print(x) # prints 10

inner_function()

outer_function()

print(x) # Error: x is not defined here

Scope Chain: A scope chain is the sequence of scopes that a variable belongs to, from the local scope to the global scope.

Example:

def outer_function():

x = 10

def inner_function():

nonlocal x

print(x) # prints 10

inner_function()

outer_function()

print(x) # Error: x is not defined here

nonlocal: The nonlocal keyword in Python allows us to modify a variable from the enclosing scope.

Example:

def outer_function():

x = 10

def inner_function():

nonlocal x

x += 1

print(x) # prints 11

inner_function()

print(x) # prints 11

global: The global keyword in Python allows us to access or modify global variables from within a function.

Example:

x = 5

def my_function():

global x

x += 1

print(x) # prints 6

my_function()

print(x) # prints 6

In summary, understanding the concept of scope in Python is crucial for writing effective and efficient code. Knowing when to use local, nonlocal, or global can help you avoid errors and make your code more readable and maintainable.

Python scope rules list

I'm happy to respond in English!

Python Scope Rules: A Comprehensive Guide

When working with variables and functions in Python, understanding the scope of those elements is crucial. Scope refers to the region of the code where a variable or function is accessible. Here's a comprehensive guide to help you navigate the complex world of Python scope rules:

Local Scope: Variables defined within a function have local scope, which means they are only accessible within that function. Once the function execution ends, these variables are garbage-collected.

Example:

def foo():

x = 10

print(x) # prints 10

foo()

print(x) # raises NameError: name 'x' is not defined

In this example, the variable x has local scope within the foo() function. Trying to access it outside the function will result in a NameError.

Global Scope: Variables declared at the top-level (outside any functions) have global scope and can be accessed from anywhere in the code.

Example:

x = 10

def foo():

print(x) # prints 10

foo()

print(x) # prints 10

In this example, the variable x has global scope. Both within and outside the function foo(), you can access it without any issues.

Nonlocal Scope: This type of scope is specific to nested functions (functions defined inside another function). A nonlocal variable is accessible from an inner function but not directly from the outer function. To access a nonlocal variable, use the nonlocal keyword in the inner function.

Example:

def foo():

x = 10

def bar():

nonlocal x

print(x) # prints 10

bar()

foo()

print(x) # raises NameError: name 'x' is not defined

In this example, the variable x has local scope within the outer function foo() but nonlocal scope within the inner function bar(). You can access x from bar(), but not directly from foo().

Built-in Scope: Python has a built-in scope that includes predefined variables and functions, such as len, print, and type. These elements are always accessible from anywhere in the code, regardless of their declaration location.

Example:

def foo():

print(len("hello")) # prints 5

foo()

In this example, the built-in function len() is accessed directly within the function foo(). You don't need to worry about scope when using built-ins.

Key Takeaways:

Local variables have scope limited to the function where they are defined. Global variables can be accessed from anywhere in the code. Nonlocal variables require the nonlocal keyword and are specific to nested functions. Built-in variables and functions are always accessible from anywhere in the code.

By understanding these scope rules, you'll write more efficient and effective Python code. Remember, Python is a dynamically-typed language that can be tricky when it comes to scoping!