SoFunction
Updated on 2024-10-30

A brief look at the proper use of Python variable scopes

When writing code, it is inevitable to use variables. However, a variable in a program is not necessarily available everywhere, and has different "scopes of validity" depending on the situation.

Look at a piece of code like this:

def func(x):
  print ('X in the beginning of func(x): ', x)
  x = 2
  print ('X in the end of func(x): ', x)
x = 50func(x)print ('X after calling func(x): ', x)

Output:

X in the beginning of func(x): 50
X in the end of func(x): 2
X after calling func(x): 50

The variable x is reassigned inside the function. But after the function is called, the value of x is still 50. why?

This brings us to the "scope" of variables:

When a variable is defined inside a function, either as a formal parameter of the function or as a separately defined variable, it acts only inside the function. Even if there is a variable outside the function with a name similar to that of the function, note that theory alone is not enough. Here by the way to send you a set of 2020 latest python introductory to advanced project combat video tutorials, you can go to the editor of the Python exchange. Skirt : seven clothes clothes nine seven seven Ba while five (the number of harmonics) conversion under can be found, but also with the old driver exchange to discuss!

The same variable is not associated with anything. The function body is the scope of the variable. Variables defined inside a function like this are called "local variables".

It's important to note that scoping starts from where the variable is defined. Writing like this is problematic:

def func():
  print (y)
  y = 2
  print (y)

Report an error:

UnboundLocalError: local variable 'y' referenced before assignment

Because y did not exist before y = 2, calling the value of y would be wrong.

Go back to that example at the beginning:

Outside the function func, the variable x is defined, assigned the value 50, and passed as a parameter to the function func. Inside the function func, the variable x is a formal parameter, and it is scoped inside the entire body of the function. It has nothing to do with the x on the outside. It has nothing to do with the x on the outside, except that its initial value is passed from the x on the outside.

So even though the x inside the function body is reassigned to 2, it doesn't affect the value of the x on the outside.

Sometimes, however, we want to be able to go inside a function and change the value of some variables that are also used outside the function. What should we do?

One way to do this is to pass the value of the changed variable as the return value of the function by using return to assign it to the corresponding variable. For example, in the beginning of the example, you could end the function with

return x

Then change the call to

x = func(x)

Another way is to use "global variables".

In Python's function definitions, you can prefix a variable name with the global keyword so that its scope is no longer confined to the function block, but is global.

Example of starting with a global rewrite:

def func():
  global x  print ('X in the beginning of func(x): ', x)
  x = 2
  print ('X in the end of func(x): ', x)
x = 50func()print ('X after calling func(x): ', x)

Output:

X in the beginning of func(x): 50
X in the end of func(x): 2
X after calling func(x): 2

The func function is no longer called with arguments. Instead, it tells the program through global x that this x is a global variable. The x in the function and the x outside become the same variable.

quantity. This time, when x is reassigned inside the function func, the external x changes as well.

The local and global variables described earlier are the most basic cases of function scoping in Python. In fact, there are slightly more complex cases, such as:

def func():
  print ('X in the beginning of func(x): ', x)  # x = 2
  print ('X in the end of func(x): ', x)
x = 50func()print ('X after calling func(x): ', x)

Output:

X in the beginning of func(x): 50
X in the end of func(x): 50
X after calling func(x): 50

The program works fine. Although global is not specified, the function uses externally defined variables internally. However, once you add the

x = 2

The program will report an error. Because at this point, x becomes a local variable whose scope begins at the point of definition and ends at the end of the function body.

It is recommended to explicitly use global variables via global while writing code to avoid using external variables directly in functions.

This is the whole content of this article.