Python 为什么我的dot输出没有被添加?然后,当我打印(点)时,它会显示add\uuuu str\uuuuuu?
为什么我的dot输出没有被添加?然后,当我调用Python 为什么我的dot输出没有被添加?然后,当我打印(点)时,它会显示add\uuuu str\uuuuuu?,python,python-3.x,Python,Python 3.x,为什么我的dot输出没有被添加?然后,当我调用print(dot)时,它会显示add\uuuu str\uuuu class Vector3D: def __init__(self, x, y,z): self.x = x self.y = y self.z = z def __add__(self, other): return Vector3D(self.x + other.x , self.y + other
print(dot)
时,它会显示add\uuuu str\uuuu
class Vector3D:
def __init__(self, x, y,z):
self.x = x
self.y = y
self.z = z
def __add__(self, other):
return Vector3D(self.x + other.x , self.y + other.y , self.z + other.z)
def __mul__(self, other):
return Vector3D(self.x * other.x , self.y * other.y , self.z * other.z)
def __str__(self):
if self.__add__(self):
return '[X = %.2f, Y = %.2f, Z = %.2f]' % (self.x , self.y, self.z)
elif self.__mul__(self):
return '%d' % (self.x + self.y + self.z)
else:
return 0
v1 = Vector3D(1,2,3)
v2 = Vector3D(4,5,6)
v3 = v1 + v2
print(v3)
dot = v1 * v2
print(dot)
不确定在
\uuuu str\uuuu
中要执行什么操作if self.\uuuu add\uuuuu(self)
与v1+v1
相同,几乎总是True
,因此\uuuu str\uuu
将返回第一个return
。欢迎光临!我不知道为什么\uu str\uuuu
函数调用add和mul,但我认为您只是想用返回'[X=%.2f,Y=%.2f,Z=%.2f]''%(self.X,self.Y,self.Z)
bool(self.\uu add(self))
->True
您得到了什么意想不到的确切输出?当我运行它时,我以[X=5.00,Y=7.00,Z=9.00][X=4.00,Y=10.00,Z=18.00]作为输出结束,这似乎很好。当调用\uuuuuu add\uuuu
或\uuuu mul\uuu
时,您需要检查一些实例变量是否设置为特定值
"[X=5.00,Y=7.00,Z=9.00]"
"32"