[Python-Dev] Callable, non-descriptor class attributes.

Steven D'Aprano steve at pearwood.info
Sat Mar 12 04:09:32 CET 2011


Thomas Wouters wrote:
> One of the things brought up at the language summit (and I believe at the VM
> summit, although I wasn't there) was the unpredictable behaviour of
> callables turning into methods when they're class attributes.
[...]
>  1. Make staticmethod a callable object directly (it isn't, currently) and
> apply it to any Python function that replaces a (in CPython) CFunction. The
> change to staticmethod may be a good idea regardless, but the policy of
> making other implementations comply to this quirk in CPython seems (to me)
> like unnecessary descrimination[*].

I've been bitten by the (to me) surprising fact that staticmethod 
objects are not directly callable. It's hard to write a method/function 
inside a class which is callable both when the class is being created, 
and afterwards, even though naively staticmethod seems like it should do 
the job. A toy example:


class Toy(object):
     @staticmethod
     def spam(n):
         return ' '.join(['spam']*n)
     lunch = spam(5) + "with a fried egg on top"


+1 on making staticmethods callable. I would have found that useful in 
the past.


>  4. Make it an error to have a callable class attribute that isn't a
> descriptor (although maybe we only discussed this one in my head.)

Do you mean to make one, or both, of these an error?

class C:
     spam = len

C.__dict__['ham'] = lambda: None




-- 
Steven



More information about the Python-Dev mailing list