首页 > 解决方案 > 在python的类函数中更改全局变量

问题描述

我以前看过有关此问题的问题,但我无法在类函数中重新创建全局变量的更改:

test = 0
class Testing:
    def add_one():
        global test
        test += 1

当我输入

Testing.add_one
print (test)

它打印“0”。如何让类中的函数添加一个进行测试?

谢谢!

标签: pythonclassglobal-variables

解决方案


尝试这个,

在此处输入图像描述

test = 0
class Testing:
    def add_one(self):
        global test
        test += 1
        print(test)

t = Testing()
t.add_one()

推荐阅读