[Python learning] Operator overload - Gukie/machine-learning GitHub Wiki
Python可以复写 操作符 比如想要复写 +, 就在类中复写对应的 add 就可以了 这也是为什么,两个int相加,得到的是数字的相加,而两个字符串相加,得到的是字符串的拼接
| Operator | Special Method | Description |
|---|---|---|
| + | add(self, object) | Addition |
| - | sub(self, object) | Subtraction |
| * | mul(self, object) | Multiplication |
| ** | pow(self, object) | Exponentiation |
| / | truediv(self, object) | Division |
| // | floordiv(self, object) | Integer Division |
| % | mod(self, object) | Modulus |
| == | eq(self, object) | Equal to |
| != | ne(self, object) | Not equal to |
| > | gt(self, object) | Greater than |
| >= | ge(self, object) | Greater than or equal to |
| < | lt(self, object) | Less than |
| <= | le(self, object) | Less than or equal to |
| in | contains(self, value) | Membership operator |
| [index] | getitem(self, index) | Item at index |
| len() | len(self) | Calculate number of items |
| str() | str(self) | Convert object to a string |