Inheritable computed class attributes?

kj at
Fri Apr 30 16:42:58 EDT 2010

I want to define a class attribute that is computed from other
class attributes.  Furthermore, this attribute should be inheritable,
and its value in the subclasses should reflect the subclasses values
of the attributes used to compute the computed attribute.  I tried
the following:

class Spam(object):
    X = 3
    def Y(cls):
        return cls.X * 3 

...but Spam.Y returns <property object at 0x......>, rather than 9.

How can I define a "class property"?  Is it possible at all?

Ultimately, I'd like to be able to define multiple subclasses of
Spam, e.g.

class Ham(Spam):
    X = 7
class Eggs(Spam):
    X = '.' 

and have Ham.Y and Eggs.Y evaluate to 21 and '...', respectively.



More information about the Python-list mailing list