[Python-ideas] staticmethod and classmethod should be callable

Nick Coghlan ncoghlan at gmail.com
Fri Jun 22 08:21:44 EDT 2018

On 21 June 2018 at 03:27, Serhiy Storchaka <storchaka at gmail.com> wrote:
> 20.06.18 20:07, Guido van Rossum пише:
>> Maybe we're misunderstanding each other? I would think that calling the
>> classmethod object directly would just call the underlying function, so this
>> should have to call utility() with a single arg. This is really the only
>> option, since the descriptor doesn't have any context.
>> In any case it should probably `def utility(cls)` in that example to
>> clarify that the first arg to a class method is a class.
> Sorry, I missed the cls parameter in the definition of utility().
> class Spam:
>     @classmethod
>     def utility(cls, arg):
>         ...
>     value = utility(???, arg)
> What should be passed as the first argument to utility() if the Spam class
> (as well as its subclasses) is not defined still?

That would depend on the definition of `utility` (it may simply not be
useful to call it in the class body, which is also the case with most
instance methods).

The more useful symmetry improvement is to the consistency of
behaviour between instance methods on class instances and the
behaviour of class methods on classes themselves.

So I don't think this is a huge gain in expressiveness, but I do think
it's a low cost consistency improvement that should make it easier to
start unifying more of the descriptor handling logic internally.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list