On 2020-09-12 at 09:57:10 +1000, Cameron Simpson firstname.lastname@example.org wrote:
@classmethod def func(cls, foo): print(foo)
A linter will warn you that "cls" is unused. With a static method:
@staticmethod def func(foo): print(foo)
a linter will be happy.
Think of @classmethod and @staticmethod as ways to write "clean" functions with no extraneous cognitive noise.
I concur with all of Cameron's technical details and explanations.
But no extraneous cognitive noise? By definition, methods appear inside a class definition, and then I have to process the @staticmethod decorator. Effectively, the decorator "cancels" the class method status of the function. I can accomplish the same thing with clean module-level function, modulo the specific namespace in which the function is created.
So, in a module m:
class X: @staticmethod def f(x): print(x)
def f(x): print(x)
m.X.f and m.f are interchangeable. IMO, m.f has less cognitive load than m.X.f, at their definitions and at call sites. Full disclosure: I consider most of Object Oriented Programming to be extraneous cognitive noise.
In other languages (*cough* Java *cough*), there are no functions outside of classes, and static methods fill that role.