On 2020-09-12 at 09:57:10 +1000,
Cameron Simpson <[email protected]> wrote:
> So, consider:
>
> @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)
and
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.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/3PEG35ADEQIEMXLKBCDGBFSM2IO5NS4V/
Code of Conduct: http://python.org/psf/codeofconduct/