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中使用全局变量的主要内容,如果未能解决你的问题,请参考以下文章

自动化测试时需要使用python,请问如何理解python中的全局变量和局部变量?

初识python: 局部变量全局变量

Python 3 实现定义跨模块的全局变量和使用

python多线程全局变量和锁

python 中怎么对一个全局的图变量在函数中进行值修改

Python实现跨文件全局变量的方法