クラス周り(2)
class Point2D: def __init__(self, x = 0, y = 0): self.x = x self.y = y def __add__(self, other): self.x += other.x self.y += other.y def GetString(self): return "(" + str(self.x) + "," + str(self.y) + ")" point1 = Point2D(10, 5) point2 = Point2D(3, 6) point1 += point2 print point1.GetString()
これもうまくいきません(´Д`;
Traceback (most recent call last): File "C:\Documents and Settings\ほげ\デスクトップ\python\hoge.py", line 14, in ? print point1.GetString() AttributeError: 'NoneType' object has no attribute 'GetString'
調べてみたところ、
ということみたいです。
つまり、
class Point2D: def __init__(self, x = 0, y = 0): self.x = x self.y = y def __add__(self, other): self.x += other.x self.y += other.y return self # ←ここ
としないと、ダメみたいですね。