Per instance descriptors ?

bruno at modulix onurb at xiludom.gro
Thu Mar 23 03:48:30 EST 2006


Michael Spencer wrote:
> Bruno Desthuilliers wrote:
> 
(snip)
> 
>> BTW, there may be other use case for per-instance descriptors... 
> 
> 
> Agreed.  Per-instance descriptors could be interesting (that's why the
> subject line caught my attention).
> But your solution involves a custom __getattribute__ in the class, which
> I would always avoid if possible (and I gather you're uneasy about it too).

I'm not uneasy about overriding __getattribute__, just about overriding
it that way -

> Here, I don't see why that's better than having a descriptor in the
> class and, if it needs per-instance behavior, then make it dependent on
> something provided by the instance.

Each instance will need it's own set of descriptors. The class is used
as a decorator for many controller functions. The descriptors are used
to encapsulate some gory implementation details about how to get such or
such object from the framework (they of course depend on instance
specific data, but that's not the point).

> e.g.,
> class DummyDescriptor1(object):
>   def __get__(self, obj, objtype=None):
>     if isinstance(obj, objtype):
>         return obj.foo.__get__(obj)
>     else:
>         return self
> 
> class MyClass4(object):
>   baaz = DummyDescriptor1()
>   def __init__(self, foo, bar = None):
>     self.foo = foo
>     self.bar = bar

This would imply a decorator subclass and a descriptor subclass for each
and every controller function - which is what I'm trying to avoid.

(snip)

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list