[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Lovepython015/Python/master/Article/PythonBasis/python10/6.md [Back]  [Original]

#  #


,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)`| = |




```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


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  {} \n'.format(num1 = num2))

```



```txt
__eq__
num1 == num2 ? --------> False

__eq__
num1 != num2 ? --------> False

__lt__
num1 < num2 ? --------> True

__gt__
num1 > num2 ? --------> False

__le__
num1  True

__ge__
num1 >= num2 ? --------> False

```

## 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)`| ``|
|`__and__(self, other)`| `&`|
|`__or__(self, other)`| `|`|
|`__xor__(self, other)`| `^`|






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



Web Proxy Viewer  |  New URL  |  Original Page