On 11Sep2020 22:58, The Nomadic Coder email@example.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).
@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 firstname.lastname@example.org