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:<br>
<br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">class Decorator:<br> def __init__():<br> self.decorated = Decorated()<br><br> def newFunction():<br>
# Do something<br> pass<br><br> def interceptRequests(request):<br> return self.decorated.request()<br><br>class Decorated:<br> def __init__():<br> self.variable = 10<br><br> def oldFunction():<br> # Do something<br>
pass</blockquote><div><br>I want to be able to do something like this:<br><br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">objectA = Decorator()<br>
objectB = Decorated()<br>assert objectA.oldFunction() == objectB.oldFunction() # No error<br></blockquote><br>Is it possible (without inheritance)?<br><br></div>--<br>Evan Kroske<br><a href="http://welcome2obscurity.blogspot.com/">http://welcome2obscurity.blogspot.com/</a><br>
The code, comments, and challenges of a novice<br>software developer desperate for attention.<br>