Home >Backend Development >Python Tutorial >What are the usages of python global?
The role of the global statement: When writing a program, if you want to reassign a variable outside a function, and this variable will act on many functions When , you need to tell python that the scope of this variable is a global variable. At this time, using the global statement can become this task, which means that the global variables cannot be modified without using the global statement.
python global usage:
Usage 1: Using global, you can make local variables in the method globally available and in other files Can also be quoted.
def fun(): global c c =0 fun() print(c) 打印: 0
Usage 2: Reassign global variables in local functions.
a = 0 b = 0 def fun(): global a # 在a 之前添加 global a = 2 b = 2 fun() print(a) print(b) 打印: 2 0
Recommended tutorial: "Python Tutorial"
The above is the detailed content of What are the usages of python global?. For more information, please follow other related articles on the PHP Chinese website!