Minor annoyances with properties

John Posner jjposner at optimum.net
Thu May 27 13:10:46 EDT 2010


On 5/27/2010 9:14 AM, Neil Cerutti wrote:
> On 2010-05-27, eb303<eric.brunel.pragmadev at gmail.com>  wrote:
>> I've been using Python properties quite a lot lately and I've
>> found a few things that are a bit annoying about them in some
>> cases. I wondered if I missed something or if anybody else has
>> this kind of problems too, and if there are better solutions
>> than the ones I'm using ATM.
>
>> The first annoyance is when I want to specialize a property in a
>> subclass.
>
> See:
>
> URI:http://infinitesque.net/articles/2005/enhancing%20Python%27s%20property.xhtml
>

Very nice idea, but I think this solution works too hard and not quite 
correctly. In Python 2.6.5, checking the name of the OProperty object's 
"fget" method:

     if self.fget.__name__ == '<lambda>' or not self.fget.__name__:

... doesn't distinguish between the original class's get-the-value 
method and the derived class's. (Did something change between 2005-11-02 
and now?)

Moreover, you don't *need* to perform this check -- just let *getattr* 
do the work of finding the right method. These method defs work fine for me:

     def __get__(self, obj, objtype):
         if self.fget:
             return getattr(obj, self.fget.__name__)()
         else:
             raise AttributeError, "unreadable attribute"

     def __set__(self, obj, value):
         if self.fset:
             getattr(obj, self.fset.__name__)(value)
         else:
             raise AttributeError, "can't set attribute"

-John



More information about the Python-list mailing list