Home  >  Article  >  Backend Development  >  python global usage

python global usage

hzc
hzcOriginal
2020-07-03 13:13:266353browse

The global usage of python is: If you need to change the variables outside the function inside the function, you can declare the variable as a global variable inside the function, so that when the program runs to the global variable, it will replace the external variable with the same name.

python global usage

#If you need to change the variables outside the function inside the function, you can declare the variable as a global variable inside the function. In this way, when the program runs to the global variable, it will replace the external variable with the same name.

Example 1:

# -*- coding:utf-8 -*-
 
name = "小明"
 
def test():
    global name
    name = "xiaoming"
    return name
 
if __name__ == "__main__":
    print name
    print test()
    print name

Run result:

小明

xiaoming

xiaoming

Recommended tutorial: "Python Tutorial"

The above is the detailed content of python global usage. 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