[Python-ideas] Good uses for staticmethod

Edward Minnix egregius313 at gmail.com
Fri Jul 22 10:31:13 EDT 2016


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160722/f1b9d2ef/attachment.html>


More information about the Python-ideas mailing list