Delegate attribute requests to object

Evan Kroske e.kroske at gmail.com
Thu Aug 13 17:50:47 CEST 2009


I'm trying to use the decorator pattern in a program I'm developing. I want
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 object.
Is there a way I can intercept all the attribute and function requests for
the decorator and delegate them to the decorated object? Here's some example
code:

class Decorator:
>   def __init__():
>    self.decorated = Decorated()
>
>  def newFunction():
>    # Do something
>    pass
>
>  def interceptRequests(request):
>    return self.decorated.request()
>
> class Decorated:
>  def __init__():
>    self.variable = 10
>
>  def oldFunction():
>    # Do something
>    pass


I want to be able to do something like this:

objectA = Decorator()
> objectB = Decorated()
> assert objectA.oldFunction() == objectB.oldFunction() # No error
>

Is it possible (without inheritance)?

--
Evan Kroske
http://welcome2obscurity.blogspot.com/
The code, comments, and challenges of a novice
software developer desperate for attention.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090813/32c1fbb8/attachment.html>


More information about the Python-list mailing list