-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex44.py
135 lines (78 loc) · 1.76 KB
/
ex44.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#implicit inheritance
class Parent(object):
def implicit(self):
print "PARENT implicit()"
class Child(Parent):
pass
dad = Parent()
son = Child()
dad.implicit()
son.implicit()
#PARENT implicit()
#PARENT implicit()
#explicit override
class Parent(object):
def override(self):
print "PARENT override()"
class Child(Parent):
def override(self):
print "CHILD override()"
dad = Parent()
son = Child()
dad.override()
son.override()
#PARENT override()
#CHILD override()
#alter before or after
class Parent(object):
def altered(self):
print "PARENT altered()"
class Child(Parent):
def altered(self):
print "CHILD, BEFORE PARENT altered()"
super(Child, self).altered()
print "CHILD, AFTER PARENT altered()"
dad = Parent()
son = Child()
dad.altered()
son.altered()
#PARENT altered()
#CHILD, BEFORE PARENT altered()
#PARENT altered()
#CHILD, AFTER PARENT altered()
#multiple inheritance => method resolution order
class SuperFun(Child, BadStuff):
pass
#super() with __init__
class Child(Parent):
def __init__(self, stuff):
self.stuff = stuff
super(Child, self).__init__()
#composition using other classes and modules
class Other(object):
def override(self):
print "OTHER override()"
def implicit(self):
print "OTHER implicit()"
def altered(self):
print "OTHER altered()"
class Child(object):
def __init__(self):
self.other = Other()
def implicit(self):
self.other.implicit()
def override(self):
print "CHILD override()"
def altered(self):
print "CHILD, BEFORE OTHER altered()"
self.other.altered()
print "CHILD, AFTER OTHER altered()"
son = Child()
son.implicit()
son.override()
son.altered()
#OTHER implicit()
#CHILD override()
#CHILD, BEFORE OTHER altered()
#OTHER altered()
#CHILD, AFTER OTHER altered()