spell method chaining?

Robin Becker robin at jessikat.fsnet.co.uk
Sat Jun 9 14:18:03 EDT 2001


In article <3B225433.11E13251 at letterror.com>, Just van Rossum
<just at letterror.com> writes
>Robin Becker wrote:
....
>
>I can't deduct from this code what on earth it's supposed to be _doing_: it's 
>way too
>convoluted for me to parse. But I'm wondering: is subclassing really the best 
>solution
>for the problem? Instead of painfully trying to dynamically create classes, why 
>don't
>you use containment and delegation? As in:
>
>class Wrapper:
>
>    def __init__(self, object):
>        self.__object = object
>    def __getattr__(self, name):
>        # this is enough for retrieving attrs, if attrs are set-able, you
>        # also need to need a corresponding __setattr__ and __delattr__
>        return getattr(self.__object, name)
>    def __getitem__(self, index):
>        return Wrapper(self.__object[index])  # whatever
>
>Just

well the idea is that if P is a property holding class so that

p=P()

p.a and p.b exist

x=TPC(P)

is an instance of P that also allows indexing

so x[0].a and x[1].b are equivalent to x.a and x.b

we can assign to indexed properties

x[0].a = 3
x[1].b = 4

then x[0].a isn't identical to x.a

assigning to x.a changes the 'default' value for all indexed values etc.
all the code works except that there's this nasty bit of chaining of
possible __getattr__ methods that could go wrong.
-- 
Robin Becker



More information about the Python-list mailing list