title: Using __setattr__ method with getattr in a class
body: <p>I have a Python class as follows:</p>
  
  <pre><code>class Base:
      def __init__(self, response=20):
          self.response = response
  
      def func1(self):
         self.response = 10
  
      def func2(self):
          self.response = 20
  
      def func3(self):
          self.response = 30
  </code></pre>
  
  <p>Every time the value of response changes, I need it to be captured. So I defined a <code>__setattr__</code> method as follows:</p>
  
  <pre><code>class Base:
      def __init__(self, response=20):
          self.response = response
  
      def __setattr__(self, name, value):
          if name == 'response':
              print('{} is set to {}'.format(name, value))
  
      def func1(self):
         self.response = 10
  
      def func2(self):
          self.response = 20
  
      def func3(self):
          self.response = 30
  </code></pre>
  
  <p>Output:</p>
  
  <pre class="lang-none prettyprint-override"><code>=============================================================
  In [10]: a = Base()
  response is set to 20
  
  In [11]: a.func1()
  response is set to 10
  
  In [12]: a.func2()
  response is set to 20
  </code></pre>
  
  <p>Works really well.</p>
  
  <p>Now I introduced one more class attribute whose value is initialized by a dictionary lookup as follows:</p>
  
  <pre><code>my_dict = {20:'key1'}
  
  class Base:
      def __init__(self, response=20):
          self.response = response
          self.key = getattr(my_dict,self.response).value
  
      def __setattr__(self, name, value):
          if name == 'response':
              print('{} is set to {}'.format(name, value))
  
      def func1(self):
          self.response = 10
  
      def func2(self):
          self.response = 20
  
      def func3(self):
          self.response = 30
  </code></pre>
  
  <p>This gives an error stating:</p>
  
  <pre class="lang-none prettyprint-override"><code> ----&gt; 6         self.key = getattr(my_dict,self.response)
  AttributeError: 'Base' object has no attribute 'response'
  </code></pre>
  
  <p>What am I doing wrong.Also if I remove the <code>__setattr__</code> method, the <code>getattr</code> starts working. What is the issue here?</p>