我收到了TypeError: 'float' object cannot be interpreted as an integer
。看起来__mul__()
正在等待一个仅有整数的参数。我没有正确定义它吗?为什么__mul __()期望整型参数?
# Running Python 3.6.0
from math import sqrt
class Vector(object):
def __init__(self, coordinates):
try:
self.dimension = len(coordinates)
if self.dimension < 2 or self.dimension > 3:
raise ValueError
self.coordinates = tuple(coordinates)
except ValueError:
raise ValueError('Must have at least 2 coordinates and no more than 3. Length = {}'.format(self.dimension))
except TypeError:
raise TypeError('The coordinates must be an iterable')
self.magnitude = sqrt(sum([n ** 2 for n in self.coordinates]))
# This fails with a type error
def normalized(self):
try:
normalized = self.coordinates.__mul__(1.0/self.magnitude)
except ZeroDivisionError:
raise Exception("Cannot normalize zero vector")
return normalized
# This fails with a type error
# def normalized(self):
# try:
# normalized = self.coordinates * (1.0/self.magnitude)
# except ZeroDivisionError:
# raise Exception("Cannot normalize zero vector")
# return normalized
# This works fine
# def normalized(self):
# try:
# normalized = [n/self.magnitude for n in self.coordinates]
# except ZeroDivisionError:
# raise Exception("Cannot normalize zero vector")
# return Vector(normalized)
def __iter__(self):
return self.coordinates
def __mul__(self, scalar):
# Vector scalar multiplication
return Vector([e * scalar for e in self.coordinates])
def __str__(self):
return 'Vector: {}'.format(self.coordinates)
# Run the test...
if __name__ == "__main__":
v1 = Vector([1.996, 3.108, -4.554])
print(v1)
print(v1.normalized())
编辑:
现在我明白了想发生了什么事我澄清答案谁在将来可能会碰上这种人。
的问题在这里:
normalized = self.coordinates.__mul__(1.0/self.magnitude)
其中,对于这种解释的目的简化为:
a = b.__mul__(c)
或
a = b * c
这里b
是一个元组和c
是一个数字,一个实数。
在Python
(1, 2) * 3
导致
(1, 2, 1, 2, 1, 2)
在*
操作者施加在元组的结果的该元组N次复制换句话说。
这也意味着我们不能用浮点数乘一个元组,这是没有意义的。因此错误:
TypeError: 'float' object cannot be interpreted as an integer
有道理。
我的错误是在我被self.magnitude
乘以元组,而不是由self.magnitude
乘我Vector
对象,像这样:
normalized = self.__mul__(1.0/self.magnitude)
鉴于我的__mul__()
定义这是有道理的,并能正常工作。而这个工程太:
normalized = self * (1.0/self.magnitude)
我想,这大概是什么OP正在努力实现比一个更好的猜测我的答案。 – DavidW
你能解释一下如何将'__mul __()'应用于元组返回多个元组的副本吗?列表理解返回一个新的Vector对象,其中包含标量乘法的结果。 –
我在文档中看不到明确的声明,但您可以测试它:'(1,2)* 2' ='(1,2,1,2)' - 就像'ab'* 2' =''abab'' –