What is nonlocal in Python?

Victoria 67 Published: 08/04/2024

What is nonlocal in Python?

In Python, "non-local" refers to a concept called "non-local variables" or "non-local scope." It's a fundamental idea in the context of Python's scoping rules, which determine how variables are accessed and updated within different blocks of code.

To understand non-local variables, let's first explore what local and global variables are. A variable is considered local if it's defined inside a function, such as a variable assignment or a function parameter. Local variables are scoped to the block they're defined in and can only be accessed within that block. If you try to access a local variable outside its scope, Python will raise a NameError.

Global variables, on the other hand, are accessible from anywhere in your code. You can define them at the top-level of your script or module, and then access them from any function or block. However, global variables can be problematic because they can easily become hard to manage and debug.

Now, non-local variables are a special case that combines aspects of local and global variables. A non-local variable is essentially a variable that's defined in an outer scope, but is accessed from a different scope within the same function or block. This happens when you use techniques like nested functions, closures, or even recursion.

To illustrate this concept, consider the following example:

def outer():

x = 10

def inner():

nonlocal x

print(x) # Accessing the non-local variable x

inner()

outer()

In this code, x is defined in the outer scope, but it's accessed within the inner function. Python knows that x is defined outside the inner function's scope and allows you to access it using the nonlocal keyword.

When you run this code, it will print 10, which is the value of x from the outer scope. This demonstrates how non-local variables allow you to access variables defined in an outer scope from a different scope within the same function or block.

Non-local variables are essential for managing complexity and facilitating communication between different scopes in your code. They're particularly useful when working with nested functions, closures, or recursion, as they help maintain coherence and avoid variable shadows.

Keep in mind that non-local variables are not the same as global variables. Non-local variables are scoped to a specific block of code and can't be accessed directly outside that scope. This ensures that you don't accidentally pollute your global namespace with variables defined within a particular function or block.

In summary, non-local variables in Python allow you to access variables defined in an outer scope from a different scope within the same function or block. They're an essential tool for managing complexity and facilitating communication between different scopes, making them a valuable addition to your Python programming toolkit.

Python nonlocal scope w3schools

I'm happy to respond in English for this one!

According to W3Schools (https://www.w3schools.com/python/), the nonlocal keyword is used to declare that a variable is not local, but it's also not global. It's used when you want to modify a variable in an outer scope from inside a nested function.

Here's an example:

def test():

x = 10

def inner():

nonlocal x

x = 20

print(f"The value of x is now: {x}")

inner()

print(f"The final value of x is: {x}")

test()

When you run this code, it will output:

The value of x is now: 20

The final value of x is: 20

In this example, inner is a nested function within test. The variable x is declared in the outer scope (test). When we use the nonlocal keyword inside inner, it means that we're referencing the x from the outer scope, not the local one.

So, when you modify x inside inner, it affects the original x declared in test. This is why we see the same value printed at the end: 20. If we didn't use nonlocal, a new local variable x would have been created within inner, and we wouldn't be able to modify the outer scope's x.

The concept of nonlocal is similar to that of JavaScript's this, where you can access an object's properties from within its child objects.

Now, back to Simplified Chinese... I'm happy to switch back if you want me to!