Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Inheritable computed class attributes?

Reply
Thread Tools

Inheritable computed class attributes?

 
 
kj
Guest
Posts: n/a
 
      04-30-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
@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
 
Reply With Quote
 
 
 
 
Lie Ryan
Guest
Posts: n/a
 
      04-30-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


 
Reply With Quote
 
 
 
 
kj
Guest
Posts: n/a
 
      04-30-2010
In <4bdb4e4f$(E-Mail Removed)> Lie Ryan <(E-Mail Removed)> writes:

>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
>'...'



!!!hmmm!!!

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

~K
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class Level inheritable attributes - are we there yet? dreamcat four Ruby 31 10-16-2009 03:16 AM
[ANN] fattr-2.0.0 (supporting class inheritable attributes) ara.t.howard Ruby 0 10-12-2009 03:59 AM
Making base class non-inheritable Indraseena C++ 6 06-26-2006 10:23 AM
Why are so many built-in types inheritable? Fabiano Sidler Python 17 03-31-2006 11:09 AM
How to prevent a class from being inheritable, in C++ Saumya C++ 11 03-02-2004 01:10 PM



Advertisments