Class Methods Vs Any Other Callable

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Thu May 15 17:25:40 EDT 2008


On 15 mai, 17:53, Arnaud Delobelle <arno... at googlemail.com> wrote:
> Bruno Desthuilliers <bruno.42.desthuilli... at websiteburo.invalid> writes:
> > FWIW, I wonder why the BDFL choosed to implement __new__ as a
> > staticmethod - there are probably some pretty good reasons, but not
> > knowing them, it looks like __new__ would have been a perfect
> > candidate for a classmethod.
>
> > So far, the only reason I can think of is that making it a classmethod
> > would have required the use of super(Parent, cls) to call the parent's
> > class __new__, which may (or may not - never had the case) be
> > problematic (any guru on this ?)
>
> Don't know but I remember something about this in 'Unifying types and
> classes'.  There it is:
>
>     Factoid: __new__ is a static method, not a class method. I
>     initially thought it would have to be a class method, and that's
>     why I added the classmethod primitive. Unfortunately, with class
>     methods, upcalls don't work right in this case, so I had to make
>     it a static method with an explicit class as its first
>     argument. Ironically, there are now no known uses for class
>     methods in the Python distribution (other than in the test
>     suite). I might even get rid of classmethod in a future release if
>     no good use for it can be found!
>
> (http://www.python.org/download/releases/2.2/descrintro/)

Thanks Arnaud. I knew there was something about this, but just
couldn't remember what. Now at least this factoid makes sense to
me :-)

Strange enough, it happened that classmethods ended being much more
used that staticmethods...

> I don't have the time to try to figure it out :(

class Foo(object):
    @classmethod
    def dothis(cls):
        print "Foo.dothis, cls is %s" % cls

    @staticmethod
    def dothat(cls):
        print "Foo.dothat, cls is %s" % cls

class Bar(Foo):
    @classmethod
    def dothis(cls):
        print "Bar.dothis, cls is %s" % cls
        try:
            Foo.dothis(cls)
        except Exception, e:
            print "trying direct call to Foo.dothis(cls), got %s" % e
        try:
            super(Bar, cls).dothis()
        except Exception, e:
            print "trying call to super(Bar, cls).dothis(), got %s" %
e

    @staticmethod
    def dothat(cls):
        print "Bar.dothat, cls is %s" % cls
        try:
            Foo.dothat(cls)
        except Exception, e:
            print "trying direct call to Foo.dothat(cls), got %s" % e
        try:
            super(Bar, cls).dothat(cls)
        except Exception, e:
            print "trying call to super(Bar, cls).dothat(), got %s" %
e

For classmethods, you *need* to use super to call the parent's class
classmethod from within the overridden child class classmethod. Which
sometimes may not be what you want for __new__. While this is a kind
of a corner case (that I never met so far, hence my interrogations
about why is __new__ a staticmethod taking the class as argument
instead of a classmethod), it is still a potential showstopper.






More information about the Python-list mailing list