2

I understood that staticmethod should always be referred to by the classname in which they belong. But I see that they can also be accessed by the keyword self too.

This is bit confusing and I don't see interpreter throwing an error.

import unittest

class TestA(unittest.TestCase):
    @staticmethod
    def fun1():
        return True

    @staticmethod
    def fun2():
        return False

    def test_one(self):
        assert TestA.fun1() == True

    def test_two(self):
        assert self.fun2() == False

if __name__ == '__main__':
    unittest.main()

What is the right way to access the staticmethod. Like TestA.fun1 above which is clear to me or as self.fun2 which is mildly concerning because there is no instance sent to fun2.

0

2 Answers 2

4

Either way is acceptable, as described in the documentation:

It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.

In some sense, the point of a staticmethod is to allow you to call the method without worrying about whether you're calling it on an instance or a class.

Sign up to request clarification or add additional context in comments.

5 Comments

Isn't the ambiguity dangerous here? First problem occurs while reading the code which has both static and instance methods scattered and you miss the purpose.
@SenthilKumaran: The purpose of a method is to do something. Name it well. Name its parameters well. If you are inside a class accessing a static method use self so when you change the class name later you don't have to fix all the calls to the static method.
@SenthilKumaran: Can you clarify what you think is dangerous? No matter what, you need to know what the method does, and knowing what it does involves knowing whether it makes use of the instance or not.
@BrenBarn To me, static means it does not change. You are having it as a helper function within the namespace of the class to be used by other instance methods. I see instance methods when they take self as arg and are called with self. (With these rules in my mind, staticmethod with self seems create some conflicts).
@SenthilKumaran: Right, and it doesn't change --- that is, the call does not depend on whether it is called on the class or the instance. If it's just creating conflicts in your conception, then you may just need to adjust your conception. You can't expect to know what self.someMethodCall() does without reading the docs for that method, and if you read the docs for that method, you will already know whether it is a staticmethod or not.
0

Either way "works", but if possible you should call using the instance. This allows it to work properly in the event that you subclass TestA. That is, it will properly find the implementation of the static method for the type of 'self' rather than the hard coded TestA. If you were in a context where you had a "cls" variable (such as a class method or perhaps factory function) you should call on that. You would only name the class object directly if you always want to call the TestA implementation, typically when you don't have a self or cls reference in the local scope.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.