Steven D'Aprano wrote:
On Sat, 22 Aug 2009 10:51:27 +0100, Jonathan Fine wrote:

Steven D'Aprano wrote:

There's a standard idiom for that, using the property() built-in, for
Python 2.6 or better.

Here's an example including a getter, setter, deleter and doc string,
with no namespace pollution, imports, or helper functions or deprecated
built-ins:

class ColourThing(object):
    @property
    def rgb(self):
        """Get and set the (red, green, blue) colours.""" return
        (self.r, self.g, self.b)
    @rgb.setter
    def rgb(self, rgb):
        self.r, self.g, self.b = rgb
    @rgb.deleter
    def rgb(self):
        del self.r, self.g, self.b

Sorry, Steve, but I don't understand this.  In fact, I don't even see
how it can be made to work.

Nevertheless, it does work, and it's not even magic. It's described (very briefly) in the docstring for property: help(property) will show it to you. More detail is here:

http://docs.python.org/library/functions.html#property

My apologies.  I wasn't up to date with my Python versions:

| Changed in version 2.6: The getter, setter, and deleter
| attributes were added.

I was still thinking Python2.5 (or perhaps earlier?). I still don't like it. All those repetitions of 'rgb'.

--
Jonathan
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to