python--global、nonlocal、闭包

1. Global Variables

In Python, a global variable is a variable that is accessible throughout the entire program, including inside functions. It can be accessed and modified by any function without the need for explicit declaration. To declare a global variable, you can simply assign a value to it outside of any function or class.

Example:

x = 10 # global variable

def func():

global x

x += 5

print(x)

func() # Output: 15

In the example above, the variable x is declared as a global variable. Inside the function func(), the global keyword is used to access and update the global variable x. When the function is called, x is incremented by 5 and the final value of x is printed.

2. Nonlocal Variables

In Python, a nonlocal variable is a variable that is accessible only within nested functions. It is a way to share variables between inner functions and their enclosing function. To declare a nonlocal variable, you can use the nonlocal keyword.

Example:

def outer():

x = 10 # local variable of outer()

def inner():

nonlocal x

x += 5

print(x)

inner() # Output: 15

outer()

In the example above, the variable x is declared as a nonlocal variable inside the inner function. The nonlocal keyword allows the inner function to access and modify the variable x from its enclosing function (outer function). When the outer() function is called, it calls the inner() function which increments the value of x by 5 and prints the final value of x.

3. Closures

A closure is a function object that remembers values in the enclosing scope even if they are not present in memory. It is a combination of a function and the environment in which it was defined. The environment consists of any local variables that were in-scope at the time the closure was created.

Closures are created by defining a function inside another function and returning the inner function. The inner function remembers the variables in the outer function even after the outer function has finished executing.

Example:

def outer_func(x):

def inner_func(y):

return x + y # x is a free variable

return inner_func

closure = outer_func(10)

print(closure(5)) # Output: 15

In the example above, the function outer_func() defines the inner function inner_func() and returns it. When we call outer_func(10), it returns the inner function as a closure. The variable x in the enclosing scope of outer_func() is remembered by the closure. We can then call the closure with an argument 5, which adds it to the remembered x value, resulting in the output 15.

The use of closures provides a way to implement data hiding and encapsulation. It allows functions to have "private" variables that cannot be accessed from outside the function.

Overall, global variables, nonlocal variables, and closures are powerful features in Python that allow for flexible variable scoping and data sharing between different parts of a program. Understanding how they work can help improve the design and readability of your code.

上一篇:python---警告框

下一篇:python---清空 clear

后端开发标签