Home  >  Article  >  Backend Development  >  What are the usages of python global?

What are the usages of python global?

烟雨青岚
烟雨青岚Original
2020-06-10 13:22:3824273browse

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!

Statement:
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