Per instance descriptors ?

Bruno Desthuilliers bdesth.quelquechose at
Thu Mar 23 00:48:50 CET 2006

Michael Spencer a écrit :
> bruno at modulix wrote:
>> Ziga Seilnacht wrote:
>>> bruno at modulix wrote:
>>>> Hi
>>>> I'm currently playing with some (possibly weird...) code, and I'd 
>>>> have a
>>>> use for per-instance descriptors, ie (dummy code):
>>> <snip>
>>>> Now the question: is there any obvious (or non-obvious) drawback with
>>>> this approach ?
> ...
>>> ...     def __getattribute__(self, name):
>>> ...         v = object.__getattribute__(self, name)
>>> ...         if not isinstance(v, types.FunctionType) \
>>                 and hasattr(v, '__get__'):
>>> ...             return v.__get__(self, self.__class__)
>>> ...         return v
> I may be missing the subtlety of what you're up to, but why is 
> overriding __getattribute__ more desirable than simply defining the 
> descriptor in a  subclass?

The code snippet I gave as an example was not supposed to reflect how I 
effectively mean to use per-instance descriptors, it was just a kind of 
Minimal Working Code (tm). The real life code is about 500 LOC, and 
explaining the whole thing would take far too long. Also, as I said, 
this is mostly syntactic sugar - there are simpler, less 'hackish' (but 
also less elegant) solutions to the actual 'problem'. So, to answer your 
question, no, subclassing would not be a solution - I'd almost need a 
subclass per controller function, which would reintroduce the 
boilerplate I'm trying to get rid of.

BTW, there may be other use case for per-instance descriptors... Python 
is so dynamic that you can almost use it like a prototype-based language.

(snip code)

More information about the Python-list mailing list