Add Properties to Instances?

Martin Miller ggrp1.20.martineau at
Mon Mar 14 10:19:23 CET 2005

In answer to my question about instance properties not working, Bengt
Richter suggest using:
> > >>> class InstProp(object):
> > ...     def __getattribute__(self, attr):
> > ...         p = object.__getattribute__(self, attr)
> > ...         if isinstance(p, property): return p.__get__(self)
> > ...         return p

and more generally for any descriptor object:

>  >>> class InstProp(object):
>  ...     def __getattribute__(self, attr):
>  ...         p = object.__getattribute__(self, attr)
>  ...         if hasattr(p, '__get__'): return p.__get__(self,
>  ...         return p

Both the above makes the '__get__' method of any property attributes of
the instance to be used. However, it does not cause attempts to be made
to access their "__set__' methods when assigning to them (which should
fail in my case because my properties don't have such a method because
they are read-only).

Just overriding '__getattribute__' alone is insufficent to make
instance property/descriptor attributes fully function. To do so also
requires overriding the __setattr__ method so it checks for a '__set__'
method and then uses it if one is found (or delegates it to
object.__setattr__ if not).

Similarily, an override for '__delattr__' would also be need for
complete property functionality, I believe.

For just making instance attributes read-only, it seems to be that the
simplist solution would be to override __setattr__ and make it check to
see if the attribute is write protected or not, which is really all I
need for the specific task I'm trying to accomplish (which is
esstentially what Jeremy Bowers said in his reply).

What still puzzles me, though, is why all the above to make properties
work on instances is necessary in the first place. It's certainly not
clear (to me) from what is said in the How-to at:
I suspect that it may be simply a performance issue, in other words, it
was considered too slow to check for instance property/discriptors --
although *why* is not clear to me.

Best Regards,

More information about the Python-list mailing list