我可以在类声明中使用重载运算符吗? 我试图在Python中创建一个简单的向量类,我意识到C++中的方法不能使用以前重载的运算符。我有什么错误吗?还是python的工作原理 class Vector3D: #constructor def __init__(self,x,y,z): self.x = x self.y = y self.z = z #overloading division operator for just numbers def __div__(self,other): if type(other) == int or type(other) == float: return Vector3D(self.x/other,self.y/other,self.z/other) print("Error") return #Used in the normalize method def Magnitude(self): return math.sqrt(self.x**2+ self.y**2 + self.z**2) # ==> using the division operator in normalization def Normalize(self): return self / self.Magnitude() # <== Throws: TypeError: unsupported operand type(s) for /: 'Vector3D' and 'float' class Vector3D: #建造师 定义初始(self,x,y,z): self.x=x self.y=y self.z=z #仅对数字重载除法运算符 定义分区(自身、其他): 如果类型(其他)==int或类型(其他)==float: 返回向量3D(self.x/other、self.y/other、self.z/other) 打印(“错误”) 返回 #用于规格化方法 def幅值(自): 返回math.sqrt(self.x**2+self.y**2+self.z**2) #==>在规范化中使用除法运算符 def正常化(自): 返回self/self.magnity() #
在Python3中,对我可以在类声明中使用重载运算符吗? 我试图在Python中创建一个简单的向量类,我意识到C++中的方法不能使用以前重载的运算符。我有什么错误吗?还是python的工作原理 class Vector3D: #constructor def __init__(self,x,y,z): self.x = x self.y = y self.z = z #overloading division operator for just numbers def __div__(self,other): if type(other) == int or type(other) == float: return Vector3D(self.x/other,self.y/other,self.z/other) print("Error") return #Used in the normalize method def Magnitude(self): return math.sqrt(self.x**2+ self.y**2 + self.z**2) # ==> using the division operator in normalization def Normalize(self): return self / self.Magnitude() # <== Throws: TypeError: unsupported operand type(s) for /: 'Vector3D' and 'float' class Vector3D: #建造师 定义初始(self,x,y,z): self.x=x self.y=y self.z=z #仅对数字重载除法运算符 定义分区(自身、其他): 如果类型(其他)==int或类型(其他)==float: 返回向量3D(self.x/other、self.y/other、self.z/other) 打印(“错误”) 返回 #用于规格化方法 def幅值(自): 返回math.sqrt(self.x**2+self.y**2+self.z**2) #==>在规范化中使用除法运算符 def正常化(自): 返回self/self.magnity() #,python,class,operator-overloading,Python,Class,Operator Overloading,在Python3中,对/使用\uuuu truediv\uuu,对/使用\uuu floordiv\uuu 这在Python2中也适用于来自uuuu future uuuuu import division的(这使得division的行为与Python3中的行为相同) 以下是另外两条建议: 首先,您可以使用模块中的类来检查数字类型,而不是检查确切的类型float和int 其次,如果重写的二进制运算符不知道如何处理给定值,则应该从该运算符返回NotImplemented。然后,Python将检查
/
使用\uuuu truediv\uuu
,对/
使用\uuu floordiv\uuu
这在Python2中也适用于来自uuuu future uuuuu import division的(这使得division的行为与Python3中的行为相同)
以下是另外两条建议:
首先,您可以使用模块中的类来检查数字类型,而不是检查确切的类型float
和int
其次,如果重写的二进制运算符不知道如何处理给定值,则应该从该运算符返回NotImplemented
。然后,Python将检查另一个操作数是否实现了操作符的翻转版本(即\uuu\r[name]\uuu
),然后重试。如果它还返回NotImplemented
,Python将引发TypeError
。这允许您创建可以在操作符两侧使用的类
import numbers
class Vector1D:
def __init__(self, x):
self.x = x
def __repr__(self, x):
return "Vector1D({})".format(self.x)
def __mul__(self, other):
if isinstance(other, numbers.Number):
return self.__class__(self.x * other)
return NotImplemented
def __rmul__(self, other):
# x * vec1d == vec1d * x
return self.__mul__(other)
def __rtruediv__(self, other):
# You can't divide things by a Vector1D
return NotImplemented
vec2 = Vector1D(2)
vec2 * 3
# Runs vec2.__mul__(2), gets Vector1D(6)
3 * vec2
# First tries int.__mul__(3, vec2), gets NotImplemented
# Then tries Vector1D.__rmul__(vec2, 3), gets Vector1D(6)
3 / vec2
# First tries int.__truediv__(3, vec2), gets NotImplemented
# Then tries Vector1D.__rtruediv__(vec2, 3), gets NotImplemented
# Python raises TypeError
在Python3中,对/
使用\uuuu truediv\uuuu
,对/
使用\uu floordiv\uuu
这在Python2中也适用于来自uuuu future uuuuu import division的(这使得division的行为与Python3中的行为相同)
以下是另外两条建议:
首先,您可以使用模块中的类来检查数字类型,而不是检查确切的类型float
和int
其次,如果重写的二进制运算符不知道如何处理给定值,则应该从该运算符返回NotImplemented
。然后,Python将检查另一个操作数是否实现了操作符的翻转版本(即\uuu\r[name]\uuu
),然后重试。如果它还返回NotImplemented
,Python将引发TypeError
。这允许您创建可以在操作符两侧使用的类
import numbers
class Vector1D:
def __init__(self, x):
self.x = x
def __repr__(self, x):
return "Vector1D({})".format(self.x)
def __mul__(self, other):
if isinstance(other, numbers.Number):
return self.__class__(self.x * other)
return NotImplemented
def __rmul__(self, other):
# x * vec1d == vec1d * x
return self.__mul__(other)
def __rtruediv__(self, other):
# You can't divide things by a Vector1D
return NotImplemented
vec2 = Vector1D(2)
vec2 * 3
# Runs vec2.__mul__(2), gets Vector1D(6)
3 * vec2
# First tries int.__mul__(3, vec2), gets NotImplemented
# Then tries Vector1D.__rmul__(vec2, 3), gets Vector1D(6)
3 / vec2
# First tries int.__truediv__(3, vec2), gets NotImplemented
# Then tries Vector1D.__rtruediv__(vec2, 3), gets NotImplemented
# Python raises TypeError
\uuuu div\uuuu
在Python3中不是一个东西。对于/
有\uuuuuuu truediv\uuuuuu
和\uuuu floordiv\uuuuu
的@user2357112,感谢我不知道:这里还有一些其他提示:如果被重写的数学运算符不知道该做什么,它们应该返回NotImplemented
(然后Python将检查其他操作数是否支持适当的操作)。与检查type(other)=
不同,您可以使用isinstance
和numbers
模块中的抽象基号类:isinstance(other,numbers.number)
@jirassimok操作已实现;TypeError
更合适,就像float一样。uu rdiv\uuu
正在提升。@chepnerfloat
的操作符不会提升TypeError
,它们返回未实现的(尝试(1.0)。\uu truediv\uu(无)
)。如果两个操作数都返回NotImplemented
,Python将自动生成TypeError
。如果您在\uuuu truediv\uuuu
中手动引发类型错误
,则会阻止其他类能够划分您的类,即使它们实现了\uuuu rtruediv\uuuuu
\uuuu div\uuuuuu
在Python 3中不是一件事。对于/
有\uuuuuuu truediv\uuuuuu
和\uuuu floordiv\uuuuu
的@user2357112,感谢我不知道:这里还有一些其他提示:如果被重写的数学运算符不知道该做什么,它们应该返回NotImplemented
(然后Python将检查其他操作数是否支持适当的操作)。与检查type(other)=
不同,您可以使用isinstance
和numbers
模块中的抽象基号类:isinstance(other,numbers.number)
@jirassimok操作已实现;TypeError
更合适,就像float一样。uu rdiv\uuu
正在提升。@chepnerfloat
的操作符不会提升TypeError
,它们返回未实现的(尝试(1.0)。\uu truediv\uu(无)
)。如果两个操作数都返回NotImplemented
,Python将自动生成TypeError
。如果您在\uuuu truediv\uuuu
中手动引发类型错误
,则会阻止其他类能够划分您的类,即使它们实现了\uuuu rtruediv\uuuu
。