[Python-Dev] Making staticmethod objects callable?

Thomas Wouters thomas at python.org
Sun Mar 12 17:51:34 CET 2006


On 3/12/06, Armin Rigo <arigo at tunes.org> wrote:
>
> Staticmethods are for the rare case where you need
> dynamic class-based dispatch but don't have an instance around.


Actually, I would argue that's what classmethods are for, not staticmethods.
You may not envision a desire for having the class in the method right now,
but it won't hurt, either. The only real use-case for staticmethods that I
know of is one Jp Caldrone pointed out once: storing arbitrary callables as
class attributes:

class MailHandleThingy(object):
    sendmail = mymaillib.mailsend
    ...

Without wrapping that in a staticmethod, 'sendmail' may or may not become a
bound method -- and that might change without touching any of the
MailHandleThingy code.
All cases where the callable is under your direct control, it's more
rewarding (same buck, way more bang) to use classmethods, IMHO.

(And no, calling a function during class-definition isn't a usecase for
staticmethods :)
--
Thomas Wouters <thomas at python.org>

Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20060312/f2b1509d/attachment.htm 


More information about the Python-Dev mailing list