6.md 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
# 六、运算符相关的魔术方法 #


运算符相关的魔术方法实在太多了,j就大概列举下面两类:


## 1、比较运算符 ##

|魔术方法|说明|
|-----|-----|
|`__cmp__(self, other)`|如果该方法返回负数,说明 `self < other`;  返回正数,说明 `self > other`; 返回 0 说明 `self == other `。强烈不推荐来定义 `__cmp__` , 取而代之, 最好分别定义 `__lt__`, `__eq__` 等方法从而实现比较功能。 `__cmp__` 在 Python3 中被废弃了。|
|`__eq__(self, other)`|定义了比较操作符 == 的行为|
|`__ne__(self, other)`|定义了比较操作符 != 的行为|
|`__lt__(self, other)`|定义了比较操作符 < 的行为|
|`__gt__(self, other)`|定义了比较操作符 > 的行为|
|`__le__(self, other)`|定义了比较操作符 <= 的行为|
|`__ge__(self, other)`|定义了比较操作符 >= 的行为|


来看个简单的例子就能理解了:

```python
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

class Number(object):
    def __init__(self, value):
        self.value = value

    def __eq__(self, other):
        print('__eq__')
        return self.value == other.value

    def __ne__(self, other):
        print('__ne__')
        return self.value != other.value

    def __lt__(self, other):
        print('__lt__')
        return self.value < other.value

    def __gt__(self, other):
        print('__gt__')
        return self.value > other.value

    def __le__(self, other):
        print('__le__')
        return self.value <= other.value

    def __ge__(self, other):
        print('__ge__')
        return self.value >= other.value


if __name__ == '__main__':
    num1 = Number(2)
    num2 = Number(3)
    print('num1 == num2 ? --------> {} \n'.format(num1 == num2))
    print('num1 != num2 ? --------> {} \n'.format(num1 == num2))
    print('num1 < num2 ? --------> {} \n'.format(num1 < num2))
    print('num1 > num2 ? --------> {} \n'.format(num1 > num2))
    print('num1 <= num2 ? --------> {} \n'.format(num1 <= num2))
    print('num1 >= num2 ? --------> {} \n'.format(num1 >= num2))

```

输出的结果为:

```txt
__eq__
3
347073565@qq.com 已提交
71
num1 == num2 ? --------> False
72 73

__eq__
3
347073565@qq.com 已提交
74
num1 != num2 ? --------> False
75 76

__lt__
3
347073565@qq.com 已提交
77
num1 < num2 ? --------> True
78 79

__gt__
3
347073565@qq.com 已提交
80
num1 > num2 ? --------> False
81 82

__le__
3
347073565@qq.com 已提交
83
num1 <= num2 ? --------> True
84 85

__ge__
3
347073565@qq.com 已提交
86
num1 >= num2 ? --------> False
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

```

## 2、算术运算符 ##

|魔术方法|说明|
|-----|-----|
|`__add__(self, other)`|实现了加号运算|
|`__sub__(self, other)`|实现了减号运算|
|`__mul__(self, other)`|实现了乘法运算|
|`__floordiv__(self, other)`|实现了 // 运算符|
|`___div__(self, other)`|实现了/运算符. 该方法在 Python3 中废弃. 原因是 Python3 中,division 默认就是 true division|
|`__truediv__(self, other)`|实现了 true division. 只有你声明了 `from __future__ import division` 该方法才会生效|
|`__mod__(self, other)`|实现了 % 运算符, 取余运算|
|`__divmod__(self, other)`|实现了 divmod() 內建函数|
|`__pow__(self, other)`|实现了 `**` 操作. N 次方操作|
|`__lshift__(self, other)`|实现了位操作 `<<`|
|`__rshift__(self, other)`|实现了位操作 `>>`|
|`__and__(self, other)`|实现了位操作 `&`|
|`__or__(self, other)`|实现了位操作 `|`|
|`__xor__(self, other)`|实现了位操作 `^`|


T
TwoWater 已提交
110 111 112 113 114 115
可以关注下公众号:

这个公号可能很少更新,但是一更新,就是把整理的一系列文章更新上去。

![](http://twowaterimage.oss-cn-beijing.aliyuncs.com/2019-10-14-WechatIMG697.jpeg)

116