thor
thor

Reputation: 1

Accessing static method variables from another static method in the same class

Facing the error as :

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

how to access the dictionary?

code:

    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()

Upvotes: 0

Views: 169

Answers (1)

snowingheart
snowingheart

Reputation: 120

Check out this on the matter of static variables in Python.

You should be able to sort it out using A.d and A.d1 whenever you wish to use the static variables. Note that, as you have them, they are local to test and test1, respectively. If you want them to be static, you have to declare them inside the class scope, but not within any function definition.

Upvotes: 1

Related Questions