Note that there are some explanatory texts on larger screens.

plurals
  1. POpython koans: class proxy
    text
    copied!<p>I'm solving <a href="https://bitbucket.org/gregmalcolm/python_koans/wiki/Home" rel="nofollow">the python koans</a>. I haven't got any real problem until the 34th.</p> <p>this is the problem:</p> <blockquote> <h1>Project: Create a Proxy Class</h1> <p>In this assignment, create a proxy class (one is started for you below). You should be able to initialize the proxy object with any object. Any attributes called on the proxy object should be forwarded to the target object. As each attribute call is sent, the proxy should record the name of the attribute sent.</p> <p>The proxy class is started for you. You will need to add a method missing handler and any other supporting methods. The specification of the Proxy class is given in the AboutProxyObjectProject koan.</p> <p>Note: This is a bit trickier that it's Ruby Koans counterpart, but you can do it!</p> </blockquote> <p>and this is my solution until now:</p> <pre><code>class Proxy(object): def __init__(self, target_object): self._count = {} #initialize '_obj' attribute last. Trust me on this! self._obj = target_object def __setattr__(self, name, value):pass def __getattr__(self, attr): if attr in self._count: self._count[attr]+=1 else: self._count[attr]=1 return getattr(self._obj, attr) def messages(self): return self._count.keys() def was_called(self, attr): if attr in self._count: return True else: False def number_of_times_called(self, attr): if attr in self._count: return self._count[attr] else: return False </code></pre> <p>It works until this test:</p> <pre><code>def test_proxy_records_messages_sent_to_tv(self): tv = Proxy(Television()) tv.power() tv.channel = 10 self.assertEqual(['power', 'channel='], tv.messages()) </code></pre> <p>where <code>tv.messages()</code> is <code>['power']</code> because <code>tv.channel=10</code> is taken by the proxy object and not the television object.<br> I've tried to manipulate the <code>__setattr__</code> method, but I always end in a unlimited loop.</p> <h3>edit 1:</h3> <p>I'm trying this:</p> <pre><code>def __setattr__(self, name, value): if hasattr(self, name): object.__setattr__(self,name,value) else: object.__setattr__(self._obj, name, value) </code></pre> <p>But then I get this error in a loop on the last entry:</p> <pre><code>RuntimeError: maximum recursion depth exceeded while calling a Python object File "/home/kurojishi/programmi/python_koans/python 2/koans/about_proxy_object_project.py", line 60, in test_proxy_method_returns_wrapped_object tv = Proxy(Television()) File "/home/kurojishi/programmi/python_koans/python 2/koans/about_proxy_object_project.py", line 25, in __init__ self._count = {} File "/home/kurojishi/programmi/python_koans/python 2/koans/about_proxy_object_project.py", line 33, in __setattr__ object.__setattr__(self._obj, name, value) File "/home/kurojishi/programmi/python_koans/python 2/koans/about_proxy_object_project.py", line 36, in __getattr__ if attr in self._count: </code></pre> <p>The loop is in <code>__getattr__</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload