Ich versuche, ein Programm zu machen Vektoren hinzuzufügen __add __
mit:'int' Objekt hat kein Attribut 'x'
class vects:
def __init__(self,x,y):
self.x = x
self.y = y
def __add__(self, vect):
total_x = self.x + vect.x
total_y = self.y + vect.y
return vects(total_x, total_y)
plusv1 = vects.__add__(2,5)
plusv2 = vects.__add__(1,7)
totalplus = plusv1 + plusv2
Der erzeugte Fehler ist wie folgt:
line 12, in <module> plusv1 = vects.__add__(2,5)
line 7, in __add__ total_x = self.x + vect.x
AttributeError: 'int' object has no attribute 'x'
Die Funktion sollten Sie verwenden wie folgt verwendet werden von dem, was ich von Ihrem Code verstanden habe: 'vects .__ __ (vects (2, 5))' und nicht 'vects .__ __ (2, 5 hinzufügen) 'da die __add__ Definition auf nur 1 Argument wartet, das eine andere Instanz der vects Klasse zu sein scheint – Artemis