[Python-Dev] Callable, non-descriptor class attributes.
Nick Coghlan
ncoghlan at gmail.com
Sat Mar 12 06:44:41 CET 2011
On Fri, Mar 11, 2011 at 10:48 PM, Guido van Rossum <guido at python.org> wrote:
>> +1 on making staticmethods callable. I would have found that useful in the
>> past.
>
> IIUC Thomas found that this breaks some current use of staticmethod.
>From his first post, I understood the compatibility issue to more be
the fact that even if we make it callable, it still needs to return
the underlying function from __get__ rather than itself. So making it
callable didn't break anything, but changing the behaviour of __get__
did.
So long as people follow the policy of applying staticmethod() when
stashing things they don't want to turn into methods in class
namespaces, that should be fine.
The difference between @staticmethod and a "@nondescriptor" decorator
that mimics CFunction behaviour would then largely be in whether or
not they implemented __get__ at all.
>>> hasattr(len, "__get__")
False
>>> hasattr(staticmethod(len), "__get__")
True
>>> staticmethod(len).__get__(type) is len
True
>>> set(dir(len)) - set(dir(staticmethod(len)))
{'__call__', '__name__', '__module__', '__self__'}
>>> set(dir(staticmethod(len))) - set(dir(len))
{'__func__', '__get__'}
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-Dev
mailing list