python 测试类全局变量写法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Dennis_Sck/article/details/86703122

实践出真知,今日份的疑惑,以下测试结论是:

  • 在类成员函数中,通过 self 定义的变量是该类全局可访问的

class testVariable(object):
    def __init__(self):
        object.__init__(self)
        self.a = 1

    def add_G(self):
        self.a += 1

    def define(self):
        self.b = 1

    def testDefine(self):
        self.b += 1


if __name__ == '__main__':
    app = testVariable()
    print("__init__, self.a = %d" % app.a)
    app.add_G()
    if app.a != 2:
        print("example one:error")
    app.define()
    if app.b != 1:
        print("example two:error")
    app.testDefine()
    if app.b != 2:
        print("example there:error")
    print("example pass all")

猜你喜欢

转载自blog.csdn.net/Dennis_Sck/article/details/86703122