Are there any benefits from using a @staticmethod?

Viewed 3770

I was wondering if you use @staticmethod decorator in your code.

Personally I don't use it, since it takes more letters to write @staticmethod then self.

The only benefit (which comes to me) from using it may be a better clarity of a code, but since I usually write a method description for sphinx, I always state whether a method is using object or not.

Or maybe I should start using @staticmethod decorator ?

4 Answers

In addition to the previous answers, from pythons doc @staticmethod :

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.

class Test:

@staticmethod
def Foo():
    print('static Foo')

def Bar():
    print('static Bar')


Test.foo() # static Foo
Test.bar() # static Bar

obj = Test()
obj.foo() # static Foo ; note that you can call it from class instance 
obj.bar() # ERROR 
Related