[Python-Dev] Declaring setters with getters

Fred Drake fdrake at acm.org
Fri Nov 2 02:18:52 CET 2007


On Oct 31, 2007, at 10:23 PM, Barry Warsaw wrote:
> BTW, +1 on this.  I like Fred's suggestion of property.set().

Thanks!  Of all the proposals that have been presented, I still like  
that the best.

Guido's use case of wanting to give the r/w property a different name  
than the r/o property is a good one, though I've not run across it  
myself.  I'd be satisfied with passing in the origin property, though  
I'd want to be able to do that for the get method as well as the set  
and delete methods.  That would better support being able to extend a  
property in a derived class.

For example:

   class Base(object):

       @property
       def attribute(self):
           return 42

   class Derived(Base):

       @property.get(Base.__dict__["attribute"])
       def attribute(self):
           self.do_something()
           return super(Derived, self).attribute

What I don't like is the difficulty of getting the raw descriptor  
from the base class.  It would be good to be able to say:

   class Derived(Base):

       @property.get(Base.attribute)
       def attribute(self):
           self.do_something()
           return super(Derived, self).attribute

I doubt that's all that hard to achieve, at least for a known  
property type.  To support descriptors of completely different types,  
the syntax from the first example may be required unless some other  
crutch is added.


   -Fred

-- 
Fred Drake   <fdrake at acm.org>





More information about the Python-Dev mailing list