[Python-Dev] Callable, non-descriptor class attributes. (original) (raw)

Steven D'Aprano steve at pearwood.info
Sat Mar 12 06:48:51 CET 2011


Guido van Rossum 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.

As I understand it, Thomas found that having staticmethod callable AND have staticmethod.get return self breaks code. His exact words were:

"With regards to changing staticmethod, in an earlier iteration of the patch I modified staticmethod to be directly callable and to return self in its tp_descr_get, and this had a side-effect I hadn't considered: code that used a staticmethod-wrapped object in any way other than calling it, broke."

I am suggesting that staticmethod should leave get as is, but add a call method equivalent to this:

def call(self, *args, **kwargs): func = self.get(self) return func(*args, **kwargs)

This won't fix the problem with inconsistent behaviour between C and Python callables in classes, but it will reduce the surprising fact that staticmethods aren't callable. Sure, it's easy enough to subclass staticmethod, but the fact that you need to is a gotcha.

This is already illegal (except for classic classes in Python 2). Also lambda creates a standard function object so it is (or has?) a descriptor.

So it is. I learn something new.

-- Steven



More information about the Python-Dev mailing list