Home > Article > Backend Development > python global usage
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.
#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!