There are several good explanations on SO about why/when you should use a class method vs a static method, but I've not been able to find an answer for when you would use a static method over no decoration at all. Consider this
class Foo(object): @staticmethod def f_static(x): print("static version of f, x={0}".format(x)) def f_standalone(x): print("standalone verion of f, x={0}".format(x)) And some output:
>>> F = Foo >>> f = F() >>> F.f_static(5) static version of f, x=5 >>> F.f_standalone(5) standalone verion of f, x=5 >>> f.f_static(5) static version of f, x=5 >>> f.f_standalone(5) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f_standalone() takes 1 positional argument but 2 were given From what I've read on here, the primary reason for using staticmethod is basically to keep conceptually similar things together. From the example above, it seems like both solutions do that. The only drawback is that you don't appear to be able to call the non-staticmethod from an instance. Maybe I'm too used to other programming languages, but this does not bother me so much; it's always surprising that I can call class-level stuff from am instance in Python.
So, is this basically the only difference between the two? Or am I missing other benefits? Thanks
f_standalone(), my first thought wouldn't be "oh, that must be a class method", but "why is he usingxand notself?!".