On 11Sep2020 22:58, The Nomadic Coder <atemysemico...@gmail.com> wrote:
>This question is to further my understanding of the internals. It seems to me 
>that a classmethod can do everything a staticmethod can, and additionally is 
>not limited by inheritance.
>
>Why does python have them as two different constructs?

It has to do with context. What context does the method need?

The default (an instance method) requires "self" to perform.

A class method requires the class to perform, but not an instance.

A static method requires neither.

Define your method appropriately.

When you start using linters to catch mechanical code issues, the 
"unused variable" is a standard issue: it means either that the function 
has been given context it doesn't need (if the variable came from a 
parameter), or that a function is doing a computation it doesn't need to 
make (or keep), or that you've got a bug because you compute something 
and fail to use it (often a variable name misspelling).

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.

Also, what's inheritance to do with this? You can inherit static 
methods, I do it all the time.  Maybe I don't understand what you mean 
by "additionally is not limited by inheritance"?

Cheers,
Cameron Simpson <c...@cskk.id.au>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/KZ6QVA3V7MZQUPGJAVNIQWQHPZQ22VUX/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to