Home > Backend Development > Python Tutorial > How Does Python's `nonlocal` Keyword Differ from `global` in Nested Function Scope?

How Does Python's `nonlocal` Keyword Differ from `global` in Nested Function Scope?

DDD
Release: 2024-12-19 22:05:11
Original
1032 people have browsed it

How Does Python's `nonlocal` Keyword Differ from `global` in Nested Function Scope?

The Power of "nonlocal" in Python 3

In Python programming, variables can be classified according to their scope:

  • Global: Accessible throughout the entire program, including nested functions.
  • Local: Defined and accessible only within a specific function or block.

However, sometimes you need to bridge the gap between these scopes. This is where the "nonlocal" keyword comes into play.

Understanding "nonlocal"

"nonlocal" is used in Python 3 to declare a variable that exists in a broader scope but is not declared as global. It allows you to access and modify variables from enclosed functions without affecting the global variables with the same name.

Impact of "nonlocal"

To illustrate the difference, consider the following example without "nonlocal":

1

2

3

4

5

6

7

8

9

10

11

12

x = 0

def outer():

    x = 1

    def inner():

        x = 2

        print("inner:", x)

 

    inner()

    print("outer:", x)

 

outer()

print("global:", x)

Copy after login

Output:

1

2

3

inner: 2

outer: 1

global: 0

Copy after login

In this case, the variable x within the inner() function is considered a new local variable, independent from the x in the outer() function and the global x.

Now, let's modify the code using "nonlocal":

1

2

3

4

5

6

7

8

9

10

11

12

13

x = 0

def outer():

    x = 1

    def inner():

        nonlocal x

        x = 2

        print("inner:", x)

 

    inner()

    print("outer:", x)

 

outer()

print("global:", x)

Copy after login

Output:

1

2

3

inner: 2

outer: 2

global: 0

Copy after login

In this scenario, the "nonlocal" keyword allows the x variable within the inner() function to reference and modify the x variable in the outer() function. The modifications made within the inner() function persist in the outer() function.

Difference from "global"

"nonlocal" differs from the "global" keyword in that it allows access and modification of variables from enclosed scopes, while "global" accesses and modifies variables from the truly global scope.

Using "global" in the above example would result in the following output:

1

2

3

inner: 2

outer: 1

global: 2

Copy after login

In this case, the x variable within the inner() function would be bound to the global x and any changes would affect the value of the global variable, rather than the one in the outer() function.

Conclusion

"nonlocal" provides a powerful mechanism for accessing and modifying variables from enclosed scopes in Python 3. It allows you to avoid the pitfalls of modifying global variables within nested functions and maintain a more organized and modular code structure.

The above is the detailed content of How Does Python's `nonlocal` Keyword Differ from `global` in Nested Function Scope?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template