Python Strange Error: "TypeError: 'NoneType' object is not callable"
<p>I'm implementing a simple class to represent a 2D vector. Here's are the relevant bits:</p>

<pre><code>class Vector:
  def __init__( self, x, y ):
    self.vec_repr = x, y

  def __add__( self, other ):
    new_x = self.x + other.x
    new_y = self.y + other.y
    return Vector( new_x, new_y )

  def __getattr__( self, name ):
    if name == "x":
      return self.vec_repr[0]
    elif name == "y":
      return self.vec_repr[1]
</code></pre>

<p>Later on, I have something like:</p>

<pre><code>a = Vector( 1, 1 )
b = Vector( 2, 2 )
a + b
</code></pre>

<p>I get <code>TypeError: 'NoneType' object is not callable</code>. This is especially strange because the error is not marked as being on any particular line, so I don't know where to look! </p>

<p>Very strange, so I did some experimentation, and discovered that it occurs on the line <code>a+b</code>. Also, when I re-work the class to be as follows:</p>

<pre><code>class Vector:
  def __init__( self, x, y ):
    self.x, self.y = x, y

  def __add__( self, other ):
    new_x = self.x + other.x
    new_y = self.y + other.y
    return Vector( new_x, new_y )
</code></pre>

<p>The error vanishes!</p>

<p>I see that there are lots of questions about an error similar to this - all seem to involve some function name being overwritten somewhere by a variable, but I don't see where this is happening!</p>

<p>As another clue, when I change the default return type of <code>__getattr__()</code> to something else - str, for example - the error morphs into <code>TypeError: 'str' object is not callable</code></p>

<p>Any ideas as to what's going on? Is there some behavior of <code>__getattr__()</code> that I don't understand?</p>
