python 在python中使用全局变量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 在python中使用全局变量相关的知识,希望对你有一定的参考价值。

from global_exp import *
import global_exp

# call the first function that is going to change the variable
func()

# call to the seonc function that is going to change the variable
func2()

# print the final result
print(main2.arg1)

# initial assignment of the variable
# can be skiped and declated it directly in the functions using the global keyword.
arg1 = 0

# changing the variable the first time
def func():
    # in order to use the global variable that is decaled in the outer scop, there is a need to use the global keyword
    global arg1
    arg1 = "changed the first time"

# changing the variable the seond time
def func2():
    # in order to use the global variable that is decaled in the outer scop, there is a need to use the global keyword
    global arg1
    arg1 = "changed the second time"

以上是关于python 在python中使用全局变量的主要内容,如果未能解决你的问题,请参考以下文章