Class Methods Vs Any Other Callable

vbgunz vbgunz at gmail.com
Wed May 14 12:21:10 EDT 2008


> > Instance methods make the most sense. A static method makes sense too
> > *but* I can see how a class method not only does what a static method
> > does but how a class method *also* gets the cls reference for free.
>
> I don't understand the last part - but I certainly agree on the "instance
> methods make the most sense". But *if* you want a hierarchy
> of "sensefulness",
>
> method > classmethod > staticmethod

OK, I am sold. If in case I find myself needing a static-method, I'll
simply make it a class-method instead. One thing that stuck out from
earlier and I'll compare it to linking is the "relative" usefulness of
cls. There are 2 conditions I can immediately come up with. 1, rename
the class and the class method still works. 2, move the method from
working on one particular class and it'll most likely work unchanged
(w/ the same interface) in another.

Other than the 2 reasons above (2 making more sense), what is a really
good reason to pull out the class method. In other words, when you see
one, what is the first thing that comes to mind? When you write one,
what was the first thing on your mind? Other than "similar to static-
methods", at what point will you be glad you used one? To sum it up,
what is the best role for a class-method that does not turn your work
into code-soup?




More information about the Python-list mailing list