Class Methods Vs Any Other Callable
vbgunz
vbgunz at gmail.com
Wed May 14 11:24:18 EDT 2008
> > An instance method works on the instance
> > A Static method is basically a function nested within a class object
> > A class method is overkill?
>
> If anything, a static method is overkill...
> class Foo:
>
> @classmethod
> def register(cls, listener):
> cls.LISTENERS.append(listener)
When I learned about static methods, I learned they're a way to
tightly couple some functionality with a class without tying the
functionality to any of the instances. I see them as nothing more than
a design decision. To me they make some sense.
Other than a methods signature (classmethod(cls, l) and a
staticmethod(l)) a class method does anything that a static method
does and gets the CLS reference for FREE? Is this why a static method
is considered to be overkill? In other words, either one can be called
from either the class or the instance and both work pretty much the
same *but* only the class method includes the class for reference and
the static method does not?
The only real difference I see between an instance and either a class
or static method is the whole bound/unbound thing. Otherwise, even an
instance can do what the others do *just* the instance method can only
make those calls through an instance and not the class.
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.
Am I correct?
More information about the Python-list
mailing list