从同一个类中的另一个静态方法访问静态方法变量

时间:2013-03-07 05:42:06

标签: python-2.7

将错误视为:

AttributeError: 'function' object has no attribute 'd'.

如何访问字典?

代码:

    class A:
    @staticmethod
    def test():
        d = {}
        d['a'] = 'b'
        print d
    @staticmethod
    def test1():
        d1 = {}
        d1['a'] = 'c'
        if (A.test.d['a'] == A.test1.d1['a']):
            print "yes"

        else:
            print "Oh No!!"
A.test()
A.test1()

1 个答案:

答案 0 :(得分:1)

在Python中查看关于静态变量问题的this

每当您希望使用静态变量时,您应该能够使用A.d和A.d1对其进行排序。请注意,正如您所拥有的那样,它们分别是test和test1的本地。如果希望它们是静态的,则必须在类范围内声明它们,但不能在任何函数定义中声明它们。