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

Guido van Rossum guido at python.org
Sat Mar 12 04:48:41 CET 2011


On Fri, Mar 11, 2011 at 10:09 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> 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.

IIUC Thomas found that this breaks some current use of staticmethod.

>>  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

Making this illegal is what #4 means, yes. But I find it unacceptable.

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

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.

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list