I am implementing a simple class to represent a 2D vector. Here are the relevant bits:
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]
Later I have something like:
a = Vector( 1, 1 ) b = Vector( 2, 2 ) a + b
I get TypeError: 'NoneType' object is not callable . This is especially strange because the error is not flagged as any particular string, so I don't know where to look!
Very strange, so I did some experiments and found that this happens on line a+b . Also, when I rework the class as follows:
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 )
The error disappears!
I see that there are many questions about an error like this - everything seems to be related to the fact that some function name is being rewritten somewhere in a variable, but I do not see where this is happening!
Like another key, when I change the default return type __getattr__() to something else - str, for example, the error turns into TypeError: 'str' object is not callable
Any ideas on what's going on? Is there some kind of __getattr__() behavior that I don't understand?