Delegate attribute requests to object
gagsl-py2 at yahoo.com.ar
Fri Aug 14 05:15:14 CEST 2009
En Thu, 13 Aug 2009 12:50:47 -0300, Evan Kroske <e.kroske at gmail.com>
> I'm trying to use the decorator pattern in a program I'm developing. I
> to create a decorator object that works like the object it's decorating
> except for a few functions. However, I'd rather not hard-code all the
> identical functionality from the decorated object into the decorator
> Is there a way I can intercept all the attribute and function requests
> the decorator and delegate them to the decorated object?
You may search the list archives for references to "proxy" and
"delegation". The term "decorator" has a very specific meaning in Python
(as a syntax construct), don't use it in the search.
> Is it possible (without inheritance)?
Yes, using delegation. See below.
En Thu, 13 Aug 2009 15:29:04 -0300, Rami Chowdhury
<rami.chowdhury at gmail.com> escribió:
> Oops, my apologies, it's the __getattribute__ method you want to call on
> self.decorated (because __getattr__ won't be there unless you define it
> So it should go:
> def __getattr__(self, request):
> return self.decorated.__getattribute__(request)
__getattribute__ doesn't exist on old-style classes either. Also, note
that very rarely one has to *call* __special__ methods; the code should
def __getattr__(self, request):
return getattr(self.decorated, request)
but that works *only* for old-style classes, as discussed here .  is
another approach for new-style classes (maybe not production-ready yet).
And this recipe  shows how to make the decorated object keep calling
methods on the decorator itself.
More information about the Python-list