-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem_25.py
executable file
·104 lines (78 loc) · 2.76 KB
/
item_25.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
#!/usr/bin/env python3
'''Item 25 from Effective Python'''
# Example 1
''' The old way to initialize a parent class from a child class is to directly
call the parent class's __init__ method with the child instance '''
class MyBaseClass(object):
def __init__(self,value):
self.value = value
class MyChildClass(MyBaseClass):
def __init__self(self):
MyBaseClass.__init__(self, 5)
# Example 2
''' define two parent classes that operate on the instance's value field '''
class TimesTwo(object):
def __init__(self):
self.value *= 2
class PlusFive(object):
def __init__(self):
self.value += 5
# Example 3
''' This class defines its parent classes in one ordering. '''
class OneWay(MyBaseClass, TimesTwo, PlusFive):
def __init__(self, value):
MyBaseClass.__init__(self, value)
TimesTwo.__init__(self)
PlusFive.__init__(self)
# Example 4
''' constructing it produces a result that matches the parent class ordering
'''
print('\nExample 4:\n==========')
foo = OneWay(5)
print('First ordering is (5 * 2) + 5 =', foo.value)
# Example 5
''' another class that defines the same parent classes but in a different
ordering '''
class AnotherWay(MyBaseClass, PlusFive, TimesTwo):
def __init__(self, value):
MyBaseClass.__init__(self, value)
TimesTwo.__init__(self)
PlusFive.__init__(self)
# Example 6
''' I left the calls to the parent class constructors PlusFive.__init__ and
TimesTwo.__init__ in the same order as before, causing this class's behavior
not to match the order of the parent classes in its definition '''
print('\nExample 6:\n==========')
bar = AnotherWay(5)
print('Second ordering still is', bar.value)
# Example 7
''' define two child classes that inherit from MyBaseClass '''
class TimesFive(MyBaseClass):
def __init__(self, value):
MyBaseClass.__init__(self, value)
self.value *= 5
class PlusTwo(MyBaseClass):
def __init__(self, value):
MyBaseClass.__init__(self, value)
self.value += 2
# Example 8
''' define a child class that inherits from both of these classes, making
MyBaseClass the top of the diamond '''
print('\nExample 8:\n==========')
class ThisWay(TimesFive, PlusTwo):
def __init__(self, value):
TimesFive.__init__(self, value)
PlusTwo.__init__(self, value)
foo = ThisWay(5)
print('Should be (5 * 5) + 2 = 27 but is', foo.value)
# Example 9
''' In Python 3, you should always use super because it's clear, concise, and
always does the right thing '''
print('\nExample 9:\n==========')
class Explicit(MyBaseClass):
def __init__(self, value):
super().__init__(value * 2)
class Implicit(MyBaseClass):
def __init__(self, value):
super().__init__(value * 2)
assert Explicit(10).value == Implicit(10).value