Make staticmethod objects callable?
Nicolas Fleury
nid_oizo at yahoo.com_remove_the_
Tue Feb 28 17:59:12 EST 2006
Steven Bethard wrote:
> Nicolas Fleury wrote:
>
>> I was wondering if it would make sense to make staticmethod objects
>> callable, so that the following code would work:
>>
>> class A:
>> @staticmethod
>> def foo(): pass
>> bar = foo()
>
> Do you have a real-world use case? I pretty much never use
> staticmethods (preferring instead to use module-level functions) so I'm
> having a hard time coming up with some code where this would be really
> useful. I'm also a little wary of breaking the parallel between
> classmethod and staticmethod which are currently *just* descriptors
> (without any other special functionality).
Well, IMHO, if staticmethod is worth being standard, than calling these
static methods inside the class definition is worth being supported. To
be honest I don't use static methods like you, but users come see me
asking "Why?" and I find their code logically structured so I conclude
there's a usability problem here.
> Maybe instead of making just staticmethods callable, both staticmethods
> and classmethods could gain an 'im_func' attribute like bound and
> unbound methods have?
I don't like it either (__get__ can be called anyway). My problem is
the expectation of the user. I wonder if it should just work. Adding a
few lines of code in staticmethod is worth it if it avoids all these
questions over and over again, no?
Regards,
Nicolas
More information about the Python-list
mailing list