一个Python类可以应用双向重载吗? [英] Can a Python class apply two-way overloading?
问题描述
现在我有一个类/对象,其中我重载了乘法运算符<$ c $
c> def __mul __(self,secondthing):所以如果我做 myObject * 4
它知道该怎么办。 但是它不知道该做什么,如果我做 4 * myObject
,反过来。
您可以实施 __ rmul __
。
调用这些方法来实现二进制算术运算(+, - ,*,/,%,divmod(),pow(),**,<< >>,&反射(交换)操作数。这些函数仅在左操作数不支持相应的操作且操作数的类型不同时才调用。
I am not sure what to call this so please bear with me.
Right now I have a class / object where I overloaded the multiplication operator def __mul__(self, secondthing):
and so if I do myObject * 4
it knows what to do with it.
But it doesn't know what to do if I do 4 * myObject
, the other way around.
You could implement __rmul__
.
These methods are called to implement the binary arithmetic operations (+, -, *, /, %, divmod(), pow(), **, <<, >>, &, ^, |) with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation and the operands are of different types.
这篇关于一个Python类可以应用双向重载吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!