Inheritable computed class attributes?

K

kj

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
@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
 
L

Lie Ryan

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:
.... X = 7
........ X = '.'
....
 
K

kj

In said:
class MetaSpam(type):
@property
def Y(cls):
return cls.X * 3
class Spam(object):
__metaclass__ = MetaSpam

and there we go:
... X = 7
...
... X = '.'
...
21
'...'


!!!hmmm!!!

That's very interesting! I did not know about metaclasses; I need
to learn more about them. Thanks for the pointer!

~K
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top