-
Notifications
You must be signed in to change notification settings - Fork 0
/
p14_oop.py
85 lines (61 loc) · 1.68 KB
/
p14_oop.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
coding=utf-8
@Author : Wu Wentong
@Time : 2021/2/23 8:19 下午
@Site :
@File : p14_oop.py
@Software: PyCharm
"""
class A:
def __add__(self, other):
print("in __add__")
print(other)
def __radd__(self, other): # 右加,对象在右边,比如 3 + a,只有算术运算有
pass
def __sub__(self, other):
pass
def __str__(self):
print("in __str__()")
def __repr__(self): ### repr和str都能打印出字符串结果,但是如果都定义了str优先级高于repr
print()
return "ddd"
def __reversed__(self):
print("in __reversed__()")
def __mul__(self, other):
pass
def __truediv__(self, other): # 除法
pass
def __divmod__(self, other): # 整除求余
pass
def __idiv__(self, other): # 存疑
pass
def __pow__(self, power, modulo=None): # **
pass
def __neg__(self): # 取反-
pass
def __xor__(self, other): # ^
pass
def __or__(self, other): # |
pass
def __and__(self, other): # &
pass
def __invert__(self): # ~
print("invert")
def __mod__(self, other): # %
print("in mod")
def __lshift__(self, other): # << 左移
pass
def __rshift__(self, other): # >> 右移
pass
if __name__ == "__main__":
a = A()
b = a + 3 # a.__add__(3)
print(b) # None
b = a - 3 # a.__sub__(3)
b = a * 3 # a.__mul__(3)
b = a / 3 # a.__truediv__(3)
# b = a // 3 # a.__idiv__(3)
b = a ** 3 # a.__pow__(3)
b = -a # a.__neg__(3)
b = ~a # a.__invert__(3)
b = a % 3 # a.__mod__(3)