[Python-Dev] Declaring setters with getters

Fred Drake fdrake at acm.org
Fri Nov 2 03:53:56 CET 2007


On Nov 1, 2007, at 9:18 PM, Fred Drake wrote:
> Thanks!  Of all the proposals that have been presented, I still like
> that the best.

I've attached a quick hack of an implementation, just to play with it  
and see how it feels.  Here's an example use:


from property import property


class Base(object):

     @property
     def attribute(self):
         print("Base.attribute (getter)")
         return 42

     @property.get(attribute)
     def rwattribute(self):
         print("Base.rwattribute (getter)")
         return self.attribute * 2

     @property.set(rwattribute)
     def rwattribute(self, value):
         print("Base.rwattribute (setter)")
         self.saved = value

class Derived(Base):

     @property.get(Base.attribute)
     def roattribute(self):
         print("Derived.roattribute (getter)")
         return self.attribute / 2



   -Fred

-- 
Fred Drake   <fdrake at acm.org>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: property.py
Type: text/x-python-script
Size: 863 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-dev/attachments/20071101/70cb9f1c/attachment.bin 
-------------- next part --------------




More information about the Python-Dev mailing list