1

In python 3, I want to do operator overloading in a class I've made.

I know __add__ stands for operator +.

What are the methods for -, *, ^, | and &?

4

1 回答 1

9

See the python docs on the data model.

The methods you asked about are: __sub__, __mul__, __xor__, __or__ and __and__

Here's the full list:

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)
于 2012-04-21T16:02:27.667 回答