What are the usages of python global?

烟雨青岚
Release: 2020-06-10 13:22:38
Original
24300 people have browsed it

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
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template