Inheritable computed class attributes?

Lie Ryan lie.1296 at gmail.com
Fri Apr 30 23:38:56 CEST 2010


On 05/01/10 06:42, kj wrote:
> 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:

I believe you will need to deal with metaclass and the descriptor
protocol (__get__/__set__) to have a computed class attribute. If anyone
knows of simpler ways, please shout out. Anyway, 'property' only works
for instances.

The "property decorator" returns the "property descriptor" object, which
has a __get__ and __set__ methods which defines how the attribute gets
accessed from its instances.

Now, remember that just like "instance" is an instance of "class", a
"class" is an instance of "metaclass". Therefore if we want to make
"class property", we need to define the property at the metaclass. Don't
worry about these details.

There are several ways to achieve what you wanted, the easiest probably
would be:

class MetaSpam(type):
    @property
    def Y(cls):
        return cls.X * 3

class Spam(object):
    __metaclass__ = MetaSpam


and there we go:

>>> class Ham(Spam):
...     X = 7
...
>>> class Eggs(Spam):
...     X = '.'
...
>>> Ham.Y; Eggs.Y
21
'...'


> class Spam(object):
>     X = 3
>     @property
>     @classmethod
>     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.
> 
> Thanks!
> 
> ~K




More information about the Python-list mailing list