Why are class methods not classmethods?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Tue Nov 27 04:21:47 EST 2007


On Tue, 27 Nov 2007 09:12:28 +0000, Steven D'Aprano wrote:

> Can anyone explain why class methods bound to a class are instancemethods 
> rather than classmethods?

I'd say because they are bound to the class which is the instance that is
passed as first argument.  Just like unbound methods turn into bound
methods when you access them on "normal" objects and change from
functions to instance methods, accessing class methods make them instance
methods too.  If you bypass the dot operator you get the still unwrapped
class method:

In [83]: Parrot.cmethod
Out[83]: <bound method type.cmethod of <class '__main__.Parrot'>>

In [84]: type(Parrot.cmethod)
Out[84]: <type 'instancemethod'>

In [85]: Parrot.__dict__['cmethod']
Out[85]: <classmethod object at 0x9b26434>

In [86]: type(Parrot.__dict__['cmethod'])
Out[86]: <type 'classmethod'>

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list