Metaclass v.s. Property function.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jens Thiede

    Metaclass v.s. Property function.

    I don't like the property function, usable in the new-style classes,
    because having to remember to manage a list of "foo = property(...)"
    assignments just plain sucks, so I wrote a metaclass that does things
    a little differently. Please have a look and tell me whether this is
    useful or impractical. The metaclass is here: http://pastebin.com/m5b06b571
    and some simple testcode is here: http://pastebin.com/m382f2ae9.
    Notice the first line though.

    Thanks for any replies,

    Jens Thiede.

  • Duncan Booth

    #2
    Re: Metaclass v.s. Property function.

    Jens Thiede <jensthiede@gma il.comwrote:
    I don't like the property function, usable in the new-style classes,
    because having to remember to manage a list of "foo = property(...)"
    assignments just plain sucks, so I wrote a metaclass that does things
    a little differently. Please have a look and tell me whether this is
    useful or impractical. The metaclass is here:
    http://pastebin.com/m5b06b571 and some simple testcode is here:
    http://pastebin.com/m382f2ae9. Notice the first line though.
    >
    Here's something I posted a while back which lets you use property as a
    decorator:

    class C(object):
    def __init__(self, colour):
    self._colour = colour

    @property.set
    def colour(self, value):
    self._colour = value

    @property.get
    def colour(self):
    return self._colour

    @property.delet e
    def colour(self):
    self._colour = 'none'

    See:


    Whether you like that style is of course a matter of personal opinion.

    Comment

    • Dustan

      #3
      Re: Metaclass v.s. Property function.

      On Aug 11, 7:33 am, Jens Thiede <jensthi...@gma il.comwrote:
      I don't like the property function, usable in the new-style classes,
      because having to remember to manage a list of "foo = property(...)"
      assignments just plain sucks, so I wrote a metaclass that does things
      a little differently. Please have a look and tell me whether this is
      useful or impractical. The metaclass is here:http://pastebin.com/m5b06b571
      and some simple testcode is here:http://pastebin.com/m382f2ae9.
      Notice the first line though.
      Are you overusing property, by any chance? In your example, bar seems
      to be an unneeded property; it could just be a normal attribute.

      Comment

      • Jens Thiede

        #4
        Re: Metaclass v.s. Property function.

        On Aug 11, 6:06 pm, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
        Jens Thiede <jensthi...@gma il.comwrote:
        I don't like the property function, usable in the new-style classes,
        because having to remember to manage a list of "foo = property(...)"
        assignments just plain sucks, so I wrote a metaclass that does things
        a little differently. Please have a look and tell me whether this is
        useful or impractical. The metaclass is here:
        http://pastebin.com/m5b06b571and some simple testcode is here:
        http://pastebin.com/m382f2ae9. Notice the first line though.
        >
        Here's something I posted a while back which lets you use property as a
        decorator:
        >
        class C(object):
        def __init__(self, colour):
        self._colour = colour
        >
        @property.set
        def colour(self, value):
        self._colour = value
        >
        @property.get
        def colour(self):
        return self._colour
        >
        @property.delet e
        def colour(self):
        self._colour = 'none'
        >
        See:http://groups.google.co.uk/group/com...e_thread/threa...
        >
        Whether you like that style is of course a matter of personal opinion.
        Thanks, a very simple but a great and more "pythonic" idea.

        Comment

        Working...