Inheritable computed class attributes?

Discussion in 'Python' started by kj, Apr 30, 2010.

  1. kj

    kj Guest

    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
     
    kj, Apr 30, 2010
    #1
    1. Advertising

  2. kj

    Lie Ryan Guest

    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
     
    Lie Ryan, Apr 30, 2010
    #2
    1. Advertising

  3. kj

    kj Guest

    In <4bdb4e4f$> Lie Ryan <> 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
     
    kj, Apr 30, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saumya
    Replies:
    11
    Views:
    4,264
    Datta Patil
    Mar 2, 2004
  2. Fabiano Sidler

    Why are so many built-in types inheritable?

    Fabiano Sidler, Mar 18, 2006, in forum: Python
    Replies:
    17
    Views:
    484
    Antoon Pardon
    Mar 31, 2006
  3. Indraseena

    Making base class non-inheritable

    Indraseena, Jun 22, 2006, in forum: C++
    Replies:
    6
    Views:
    481
    Indraseena
    Jun 26, 2006
  4. dreamcat four
    Replies:
    31
    Views:
    362
    ara.t.howard
    Oct 16, 2009
  5. ara.t.howard
    Replies:
    0
    Views:
    103
    ara.t.howard
    Oct 12, 2009
Loading...

Share This Page