classmethod() not inherited?

Martin v. Löwis martin at
Thu Jan 16 11:25:37 CET 2003

Jack Diederich <jack at> writes:

> How is it possible to have classmethods still be
> classmethods in their kids?

As you found out, you have to explicitly generate class methods,

> I'd like to have ALL of the derivitives of Base
> have foo as a classmethod, but it doesn't seem
> to be automatic and the following doesn't work
> import Test
> for (Test.A .. Test.Z): # pseudo code
> = classmethod(

This doesn't work because the classmethod builtin does not really
accept method objects. It would work if you write either = classmethod(

or = classmethod(Test.A.__dict__['foo'])

I readily admit that both constructs are hacks that make unacceptable
use of implementation details :-)

However, you may reconsider your need to redefine the class method in
every subclass. The whole point of class methods is that they get the
class as argument, which, in many cases, can help to avoid
redefinitions, e.g. in

class Person:
  minage = 0
  maxage = 100
  def span(self):
    return self.maxage-self.minage

class Child:
  maxage = 18

class Adult:
  minage = 18

Now, the .span method works fine for all three classes.


More information about the Python-list mailing list