title: Class properties and __setattr__
body: <p>In Python class, when I use <code>__setattr__</code> it takes precedence over properties defined in this class (or any base classes). Consider the following code:</p>
  
  <pre><code>class Test(object):
      def get_x(self):
          x = self._x
          print "getting x: %s" % x
          return x
      def set_x(self, val):
          print "setting x: %s" % val
          self._x = val
      x = property(get_x, set_x)
      def __getattr__(self, a):
          print "getting attr %s" % a
          return -1
      def __setattr__(self, a, v):
          print "setting attr %s" % a
  </code></pre>
  
  <p>When I create the class and try to set <code>x</code>, <code>__setattr__</code> is called instead of <code>set_x</code>:</p>
  
  <pre><code>&gt;&gt;&gt; test = Test()
  &gt;&gt;&gt; test.x = 2
  setting attr x
  &gt;&gt;&gt; print test.x
  getting attr x_
  getting x: -1
  -1
  </code></pre>
  
  <p>What I want to achieve is that the actual code in <code>__setattr__</code> were called only if there is no relevant property i.e. <code>test.x = 2</code> should call <code>set_x</code>. I know that I can achieve this easily by manually checking if <code>a</code> is "<em>x</em>" is <code>__setattr__</code>, however this would make a poor design. Is there a more clever way to ensure the proper behavior in <code>__setattr__</code> for every property defined in the class and all the base classes?</p>