20. Python — variable scope, local functions

Directory

  • variable scope
    • variable? dictionary?
    • Get variable dictionary
    • variable masking
      • Solution:
        • Method 1: Use globals to access global variables
        • Method 2: Declare global variables in the function
  • local function
    • Enclosing function returns local function
      • code demo
      • Another way to write
    • Masking of local functions
      • The problem is as shown in the figure:
      • Solution:
    • Summary of global and nonlocal

Understanding variable scope
variable dictionary
Handle local variables and shadow global variables
Understand local functions
Define and use local functions
Enclosing function returns local function
Variable masking of local functions

Variable scope

Depending on where the variable is defined, there are two types of scope:

Local variables: Variables defined in a function, including parameters, are called local variables.
Global variables: Variables defined outside the function and in the global scope are called global variables.

Variable? dictionary?

Regardless of whether it is global scope or local scope, these variables and their values are like an “invisible” dictionary:

The variable name is the key of the dictionary
The variable value is the value of the dictionary.

Get variable dictionary

globals(): This function returns a “variable dictionary” consisting of all variables in the global scope.

locals(): This function returns a “variable dictionary” consisting of all variables in the current local scope.

vars(object): Gets the “variable dictionary” composed of all variables in the scope of the specified object. If the object parameter is not passed in, vars() and locals() have exactly the same effect.

Explanation: Why the results of printing globals and locals globally are the same
As shown in the picture:
locals gets all local variables in the current scope, so if locals() is called in the global scope, it returns all global variables.
To put it simply, in the global scope (in the global scope, its current scope is the global), the effect of using the globals() and locals() functions is the same.

globals()

Note that calling the globals() function inside a function can still only obtain global variables, but not local variables

locals()

For local variables, you should use the locals() function to obtain them. The locals() function returns a dictionary that contains all variables in the current scope, including local variables

Variable masking

Global variables can be accessed within all functions by default.

If a variable with the same name as a global variable is defined in a function, the local variable will hide the global variable.

As shown in the picture:

Solution:

Method 1: Use globals to access global variables

I hope to use global variables in the place of global variables and local variables in the place of local variables.
That is, in global variables, name is aaaaaa, and in local variables, name is bbbbbbb.

Shaded global variables can be accessed in functions through globals()

Method 2: Declare global variables in the function

You can use the global statement in a function to declare global variables, which avoids redefining local variables and instead assigns values to global variables.

Note: If you want to assign a value to a global variable within a function, you need to use global to declare the global variable.

That is, when operating a name variable with the same name in a function, you are operating the name of the global variable, rather than redefining the name variable in method 1.

=================================================

Local function

Functions defined within a function body are called local functions.
It’s a function inside a function, it’s called a local function.

By default, local functions are hidden from the outside, and local functions can only be used within their enclosing function.

Closed function returns local function

Enclosing functions can return local functions so that the program can use them in other scopes.

If the enclosing function does not return the local function, the local function will only be called within the enclosing function.

Code demonstration

Requirement: Return the local function bar in the info function so that the program can use the local function bar in other scopes
Originally, the bar function could only be called inside the info function. Now with this operation, the bar function can be called outside the info function, because the info function returns the bar function as a return value.

This info is a closed function, also called an external function.

Explanation:

Another way of writing

How to write the local function bar outside the info function

Shading of local functions

Variables within a local function also shadow local variables of the enclosing function in which it resides.

In order to avoid blocking the local variables of the enclosing function, local functions can be declared using nonlocal

The meaning of masking, I personally understand it as eliminating other people’s variables. It doesn’t mean coverage. But it’s gone.

The problem is as shown below:

As shown in the picture: The mask here is the name variable that I can originally call to the test closed function (external function) in the info internal function. The name variable at this time is Local variables belonging to the test function.
But if I define a name variable with the same name in the info function, then the name variable of the info function will be shadowed by the name variable of the test function.
Therefore, the name referenced in the first line of the info function could originally be referenced to the name variable of the test function. Now because it is obscured, it is shown that the name reference has not been resolved. It is simply understood that there is no such name variable.

Solution:

In order to avoid blocking the local variables of the enclosing function, local functions can be declared using nonlocal

Summary of global and nonlocal

The nonlocal function is roughly similar to the global function introduced earlier: both are used to avoid variables being obscured.

The difference is that global is used to declare access to global variables, while nonlocal is used to declare access to local variables within the enclosed function where the local function is located.