Making a decorator a staticmethod

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Jan 8 15:21:07 EST 2009


Jonathan Gardner a écrit :
> On Jan 8, 11:18 am, "Zac Burns" <zac... at gmail.com> wrote:
>> In my use case (not the example below) the decorator returns a
>> function of the form def f(self, *args, **kwargs) which makes use of
>> attributes on the instance self. So, it only makes sense to use the
>> staticmethod in the class and in the baseclass. Making this decorator
>> a module level function doesn't make sense here.
>>
> 
> I don't think you should be using staticmethod in this case since you
> are relying on information in the class itself. 

Nope. He's relying on (part of) the interface(s) implemented by the 
first argument. The class object itself has nothing to do with is 
(well... it does, but only as far as it contribute to the implementation 
of the interface expected by the decorator).


> (Aside: I really can't think of any reason to use staticmethods in
> Python other than to organize functions into namespaces, and even
> then, that's what modules are for, right?)

I sometimes found staticmethods to be useful, in that they provided 
polymorphic dispatch without having to care about the containing module 
(or whether the object thru which the staticmethod_or_function is 
accessed is a module, class or instance).


> I think you need to show a better example of what it is you are trying
> to do.

+1 on this. So far, the only concrete use case for staticmethod as a 
decorator I can think of is the one exposed above for staticmethods in 
general.





More information about the Python-list mailing list