python 支持通过对一些魔法方法的重写 可以实现自定义
如算术运算
__add__(self, other) | + |
__sub__(self, other) | - |
__mul__(self, other) | * |
__truediv__(self, other) | / |
__floordiv__(self, other) | // |
__mod__(self, other) | % |
__power__(self, other) | ** |
略 |
|
、
一元操作符
__neg__(self) | 定义正号行为 |
__pos__(self) | 定义负号行为 |
__abs__(self) | 定义当被abs()调用时的行为 |
__invert__(self) | 定义求反行为 |