Dies ist meine vereinfachte Klassenzuordnung:Python Komplizierte Zahl Multiplikation
class Vector(object):
def __init__(self, x, y):
self.x = x
self.y = y
class MyComplex(Vector):
def __mul__(self, other):
return MyComplex(self.real*other.real - self.imag*other.imag,
self.imag*other.real + self.real*other.imag)
def __str__(self):
return '(%g, %g)' % (self.real, self.imag)
u = MyComplex(2, -1)
v = MyComplex(1, 2)
print u * v
Dies ist die Ausgabe:
"test1.py", line 17, in <module>
print u * v
"test1.py", line 9, in __mul__
return MyComplex(self.real*other.real - self.imag*other.imag,
self.imag*other.real + self.real*other.imag)
AttributeError: 'MyComplex' object has no attribute 'real'
Der Fehler ist klar, aber ich nicht, es herauszufinden, Ihre Hilfe bitte!
Es sollte auch darauf hingewiesen werden, dass Sie dadurch die Notwendigkeit vermeiden, dass MyKomplex von "Vector" erbt. – Billylegota