Totally agree with this.  I think both of points should be clarified in the documentation: https://docs.python.org/3/library/functions.html#staticmethod

Namely that:
* Nearly all uses of static methods should instead be class methods, and
* The main exception is setting an ordinary function as a class attribute without making it a method.

Best,

Neil
On Sunday, December 19, 2021 at 3:07:38 PM UTC-5 Serhiy Storchaka wrote:
19.12.21 19:41, Ethan Furman пише:
> On 12/19/21 5:40 AM, Serhiy Storchaka wrote:
>
>> classmethod supersedes staticmethod. It was not clearly known when they
>> were introduced, but now we see that there is very few use cases for
>> staticmethod which cannot be replaced by classmethod (actually only one
>> specific case).
>
> What is the one case?

Setting a Python function as a class attribute without making it a method.

def func(*args, **kwargs): ...
class A:
f = staticmethod(func)
a = A()
a.f(1) # calls func(1), not func(a, 1)

It is used in tests if there are C and Python implementations.
staticmethod(func) could be replaced with

lambda self, /, *args, **kwargs: func(*args, **kwargs)

or

property(lambda self: func)

in these cases.

_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/J7GEEWCKIXWXOEQ4KD3ZFPA5W73ZAWVK/
Code of Conduct: http://python.org/psf/codeofconduct/