[Python-Dev] Declaring setters with getters

Fred Drake fdrake at acm.org
Thu Nov 1 02:38:27 CET 2007


On Oct 31, 2007, at 4:28 PM, Guido van Rossum wrote:
> Given how rarely supporting deletions matters enough to write extra
> code, we can just say that *when using @propset* your setter function
> needs to have a default value for the argument or otherwise support
> being called with one or two arguments.

It's definitely unusual, but the logic is typically very different;  
conflating the method in Python doesn't really feel "right" to me.

I've been using Philipp von Weitershausen's "rwproperty" quite happily:

   http://pypi.python.org/pypi/rwproperty/

It uses the names "getproperty", "setproperty", and "delproperty",  
which feel reasonable when I use them (always referenced using the  
module), like so:

   class Thing(object):

       @rwproperty.setproperty
       def attribute(self, value):
           # ...

If I had to choose built-in names, though, I'd prefer "property",  
"propset", "propdel".  Another possibility that seems reasonable  
(perhaps a bit better) would be:

   class Thing(object):

       @property
       def attribute(self):
           return 42

       @property.set
       def attribute(self, value):
           self._ignored = value

       @property.delete
       def attribute(self):
           pass


   -Fred

-- 
Fred Drake   <fdrake at acm.org>





More information about the Python-Dev mailing list