Good uses for staticmethod

Hello, I have been reading Luciano Ramalho’s Fluent Python, and in the book he says he cannot think of a good use for staticmethod, I have been thinking about that, and so far I have only thought of one good use case: when you have a version of a common function, e.g., sum, and you want to qualify it with a better name but something like sumLineItems would sound wrong (to me at least). Would this be a good choice for something like: class LineItem: . . . . . . . . def __add__(self, other): ### Function body ### . . . . . . . . @staticmethod def sum(items: Sequence[LineItem]) -> LineItem: ### Add up LineItems ### Does this seem like a reasonable use for staticmethod? I think its a good way to qualify a name in a more concise way. Thanks, Ed M.

On Fri, 22 Jul 2016 at 07:34 Edward Minnix <egregius313@gmail.com> wrote:
I don't think it is as I don't think it's any more concise than sum_line_items(). The only reason I ever use staticmethod is if I have a method that doesn't use self or cls but I expect subclasses to potentially need/want to override the method. Otherwise whatever the staticmethod is should be a function.

Honestly, staticmethod was something of a mistake -- I was trying to do something like Java class methods but once it was released I found what was really needed was classmethod. But it was too late to get rid of staticmethod.

On Fri, Jul 22, 2016, at 13:51, Guido van Rossum wrote:
Regardless of its utility or lack thereof for methods, staticmethod seems to me to be the best way to make an attribute of a class that happens to be a function (or, theoretically, some other other descriptor object). I like to think of it as an "implicit-magic remover". Probably if it didn't exist something like it would have been proposed eventually, under some other name like @funcattr or something.

There is no module or namespace keyword in python. One module consists at least one file. But sometimes I'm too lazy to create multiple files, or I simply cannot, e.g. when writing a plugin for something. I write staticmethod as a replacement of module function in these cases. 2016-07-23 2:41 GMT+08:00 Random832 <random832@fastmail.com>:

On Fri, 22 Jul 2016 at 07:34 Edward Minnix <egregius313@gmail.com> wrote:
I don't think it is as I don't think it's any more concise than sum_line_items(). The only reason I ever use staticmethod is if I have a method that doesn't use self or cls but I expect subclasses to potentially need/want to override the method. Otherwise whatever the staticmethod is should be a function.

Honestly, staticmethod was something of a mistake -- I was trying to do something like Java class methods but once it was released I found what was really needed was classmethod. But it was too late to get rid of staticmethod.

On Fri, Jul 22, 2016, at 13:51, Guido van Rossum wrote:
Regardless of its utility or lack thereof for methods, staticmethod seems to me to be the best way to make an attribute of a class that happens to be a function (or, theoretically, some other other descriptor object). I like to think of it as an "implicit-magic remover". Probably if it didn't exist something like it would have been proposed eventually, under some other name like @funcattr or something.

There is no module or namespace keyword in python. One module consists at least one file. But sometimes I'm too lazy to create multiple files, or I simply cannot, e.g. when writing a plugin for something. I write staticmethod as a replacement of module function in these cases. 2016-07-23 2:41 GMT+08:00 Random832 <random832@fastmail.com>:
participants (5)
-
Brett Cannon
-
Edward Minnix
-
Guido van Rossum
-
Random832
-
王珺