Skip to main content
 首页 » 编程设计

python之__add__ 的对立面

2025年05月04日10dflying

在 python 3 中,我想在我创建的类中进行运算符重载。

我知道__add__代表运算符+。

-的方法是什么? , * , ^ , |和 & ?

请您参考如下方法:

参见 the python docs on the data model .

您询问的方法是:__sub__、__mul__、__xor__、__or__ 和 __and__

完整列表如下:

object.__add__(self, other) 
object.__sub__(self, other) 
object.__mul__(self, other) 
object.__truediv__(self, other) 
object.__floordiv__(self, other) 
object.__mod__(self, other) 
object.__divmod__(self, other) 
object.__pow__(self, other[, modulo]) 
object.__lshift__(self, other) 
object.__rshift__(self, other) 
object.__and__(self, other) 
object.__xor__(self, other) 
object.__or__(self, other)