Python类可以应用双向重载吗?

2024-04-23 10:07:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道该怎么称呼这件事,所以请容忍我。你知道吗

现在我有一个类/对象,我重载了乘法运算符def __mul__(self, secondthing):,所以如果我重载了myObject * 4,它就知道该怎么处理它了。你知道吗

但它不知道如果我做了4 * myObject该怎么办,反之亦然。你知道吗


Tags: 对象selfdef运算符乘法mulmyobjectsecondthing
1条回答
网友
1楼 · 发布于 2024-04-23 10:07:18

您可以实现^{}。你知道吗

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.

相关问题 更多 >