Repository navigation
Expand file tree
/
Copy pathEasy Class Python Program Examples Six.py
More file actions
382 lines (258 loc) · 13.3 KB
/
Copy pathEasy Class Python Program Examples Six.py
File metadata and controls
382 lines (258 loc) · 13.3 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# Easy Class Python Program Examples Six:
# by Joseph C. Richardson,GitHub.com
# Welcome to our last Easy Class Python program examples:
# volume six, when we can fully understand C3 Linearization
# and the 'DIAMOND PROBLEM' with class inheritance.
# As we go, we will learn how to keep our code completely DRY!:
# (Don't Repeat Yourself!). For now, let's take things slow and
# easy. Most importantly: Do not over concentrate. Take your
# time to learn Python. Now, let's WAX on and WAX off. Shall we...
# Let's learn what C3 Linearization is all about.
# C3 Linearization is a type of MRO: (Method Resolution Order)
# when one or more classes inherit parent classes. Note: these
# easy class examples do not require class constructors or
# attributes. In our first easy class example, we do not have to
# worry about C3 Linearization, MRO: (Method Resolution Order)
# in the final Child class act.
class Grandmother: # parent class
def grandmother(self): # define function
print('Mary')
class Grandfather: # sub parent class
def grandfather(self): # define function
print('Jerry')
class Mom: # sub parent class
def mom(self): # define function
print('Jane')
class Dad: # sub parent class
def dad(self): # define function
print('John')
class Child(Grandmother,Grandfather,Mom,Dad):pass
# Each class can be called up by itself; we can gain direct
# access to individual classes, along with a dot notation and
# def function name, associated with that individual class.
# Grandmother.grandmother('')
Grandmother.grandmother('argument placeholder value')
Grandfather.grandfather('argument placeholder value')
Mom.mom('argument placeholder value')
Dad.dad('argument placeholder value')
# We can use the Child class alone to call up any classes we
# like. Remember the dot notation, along with the associated
# def function name.
Child.grandmother('argument placeholder value')
Child.grandfather('argument placeholder value')
Child.mom('argument placeholder value')
Child.dad('argument placeholder value')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Here is when C3 Linearization, MRO: (Method Resolution Order)
# does matter. Let's call this 'Diamond Problem', "Class-Incest",
# when a sub parent class inherits a parent class above it. For
# example, let's make the Grandfather(Grandmother) become a
# child class of the Grandmother class.
# Note: for classes to work correctly, they have to contain at least
# one define function, so the class waits to be accessed. If def functions
# are not within a class, the class will act as a bottomless cup; it won't
# wait to be accessed. Here is a quick refresh for those, who are just
# learning how to create classes without class constructors or class
# attribute properties.
def my_def_function(): # define function
print('Mary')
my_def_function() # Call up the def function: my_def_function()
# Here is the very same def function() example, but we need the
# 'self' parameter when invoking def functions within classes.
def my_def_function(self): # define function
print('Mary')
# Call up the def function: my_def_function('argument placeholder value')
my_def_function('argument placeholder value')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's try one class-incest-diamond problem and see what happens
# when the Grandfather, becomes the child class act of the Grandmother
# class.
class Grandmother: # non class-incest parent class
def grandmother(self): # define function
print('Mary')
class Grandfather(Grandmother): # single class-incest child
def grandfather(self): # define function
print('Jerry')
class Mom: # non class-incest sub parent class
def mom(self): # define function
print('Jane')
class Dad: # non class-incest sub parent class
def dad(self): # define function
print('John')
# When a class-incest child happens, we must make sure that the child
# class, Grandfather has to stay to the left side of its non class-incest
# parent class, or non class-incest sub parent class: Grandmother, in
# the final non class-incest Child class below is where this matters.
# Grandfather must stay to the left of any non class-incest parent, or
# non class-incest sub parent: Grandmother for example.
class Child(Grandfather,Grandmother):pass # Oh, you poor Child class act. lol
Child.grandmother('argument placeholder value')
Child.grandfather('argument placeholder value')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# All "class-incest" sub child classes cannot step over the boundary
# of any non-incest parent classes. An MRO conflict will occur.
class Grandmother: # non class-incest parent class
def grandmother(self): # define function
print('Mary')
class Grandfather(Grandmother): # single class-incest child
def grandfather(self): # define function
print('Jerry')
class Mom(Grandmother): # single class-incest child
def mom(self): # define function
print('Jane')
class Dad: # non class-incest sub parent class
def dad(self):
print('John')
class Child(Mom,Grandfather,Grandmother):pass # Oh, you poor Child class act. lol
# All class-incest children can be swapped in any order, as long
# as to never overstep the boundary of their parent class.
class Child(Grandfather,Mom,Grandmother):pass # Oh, you poor Child class act. lol
# The non class-incest Dad class can float around all he wants.
# He's not a parent class at all. Dad can also go in between class-incest
# classes all he likes. Dad can go from one end, to the other if he likes.
class Child(Dad,Mom,Grandfather,Grandmother):pass # Oh, you poor Child class act. lol
class Child(Dad,Grandfather,Mom,Grandmother):pass # Oh, you poor Child class act. lol
class Child(Mom,Dad,Grandfather,Grandmother):pass # Oh, you poor Child class act. lol
class Child(Mom,Grandfather,Dad,Grandmother):pass # Oh, you poor Child class act. lol
class Child(Mom,Grandfather,Grandmother,Dad):pass # Oh, you poor Child class act. lol
class Child(Grandfather,Mom,Grandmother,Dad):pass # Oh, you poor Child class act. lol
# The class-incest Mom child class overstepped her boundary.
# All class-incest children classes can never overstep their non-
# incest parent classes. The incest-class Mom class oversteps
# her non class-incest parent class, Grandmother. Class-incest
# Grandfather class and class-incest Mom classes are Grandmother's
# children now, through incest.
class Child(Grandfather,Grandmother,Mom,Dad):pass # MRO: Method Resolution Order error
Child.grandmother('argument placeholder value')
Child.grandfather('argument placeholder value')
Child.mom('argument placeholder value')
Child.dad('argument placeholder value')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's step back and explain what 'self' is and why 'self' is needed
# within the def function's parameter. The 'self' parameter always
# starts at position zero, not position one. The 'self' parameter
# ensures that each def function stays defined within its own class
# scope, locking the function directly to the class structure. The
# argument placeholder values assure that each defined function
# can be called through the argument placeholder values.
# Note: argument placeholder values do not have to acquire text
# values at all. However, we need to invoke two single quote marks
# (''), or two double quote marks ("") to satisfy the self parameter.
# Child.grandmother('')
# Child.grandfather('')
# Child.m('')
# Child.d('')
# Child.grandmother("")
# Child.grandfather("")
# Child.m("")
# Child.d("")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Here are three 'class-incest' child classes that inherit the Grandmother
# class.
class Grandmother: # non class-incest parent class
def grandmother(self): # define function
print('Mary')
class Grandfather(Grandmother): # single class-incest child
def grandfather(self): # define function
print('Jerry')
class Mom(Grandmother): # single class-incest child
def mom(self): # define function
print('Jane')
class Dad(Grandmother): # single class-incest child
def dad(self): # define function
print('John')
class Child(Mom,Grandfather,Dad,Grandmother):pass # Oh, you poor Child class act. lol
class Child(Grandfather,Mom,Dad,Grandmother):pass # Oh, you poor Child class act. lol
class Child(Grandfather,Dad,Mom,Grandmother):pass # Oh, you poor Child class act. lol
class Child(Dad,Grandfather,Mom,Grandmother):pass # Oh, you poor Child class act. lol
class Child(Mom,Dad,Grandfather,Grandmother):pass # Oh, you poor Child class act. lol
class Child(Dad,Mom,Grandfather,Grandmother):pass # Oh, you poor Child class act. lol
# The incest-class, Grandfather class has overstepped his parent,
# the Grandmother class.
class Child(Dad,Mom,Grandmother,Grandfather):pass # MRO: Method Resolution Order error
Child.grandmother('')
Child.grandfather('')
Child.mom('')
Child.dad('')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Try these class-incest/diamond problem Python program examples:
class Grandmother: # non class-incest parent class
def grandmother(self): # define function
print('Mary')
class Grandfather: # non class-incest sub parent class
def grandfather(self): # define function
print('Jerry')
class Mom: # non class-incest sub parent class
def mom(self): # define function
print('Jane')
class Dad(Mom,Grandfather): # double class-incest child
def dad(self): # define function
print('John')
class Child(Dad,Mom,Grandfather,Grandmother):pass # Oh, you poor Child class act. lol
class Child(Dad,Mom,Grandmother,Grandfather):pass # Oh, you poor Child class act. lol
class Child(Dad,Grandmother,Mom,Grandfather):pass # Oh, you poor Child class act. lol
class Child(Grandmother,Dad,Mom,Grandfather):pass # Oh, you poor Child class act. lol
class Child(Mom,Dad,Grandfather,Grandmother):pass # MRO: Method Resolution Order error
class Child(Dad,Grandfather,Mom,Grandmother):pass # MRO: Method Resolution Order error
class Child(Grandfather,Dad,Mom,Grandmother):pass # MRO: Method Resolution Order error
Child.grandmother('')
Child.grandfather('')
Child.mom('')
Child.dad('')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Grandmother: # non class-incest parent class
def grandmother(self): # define function
print('Mary')
class Grandfather: # non class-incest sub parent class
def grandfather(self): # define function
print('Jerry')
class Mom(Grandfather): # single class-incest child
def mom(self): # define function
print('Jane')
class Dad(Mom,Grandfather): # double class-incest child
def dad(self): # define function
print('John')
class Child(Dad,Mom,Grandfather,Grandmother):pass # Oh, you poor Child class act. lol
Child.grandmother('')
Child.grandfather('')
Child.mom('')
Child.dad('')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Grandmother: # non class-incest parent class
def grandmother(self): # define function
print('Mary')
class Grandfather: # non class-incest sub parent class
def grandfather(self): # define function
print('Jerry')
class Mom: # non class-incest sub parent class
def mom(self): # define function
print('Jane')
class Dad(Mom,Grandfather,Grandmother): # triple class-incest child
def dad(self): # define function
print('John')
class Child(Dad,Mom,Grandfather,Grandmother):pass # Oh, you poor Child class act. lol
class Child(Dad,Mom,Grandmother,Grandfather):pass # MRO: Method Resolution Order error
Child.grandmother('')
Child.grandfather('')
Child.mom('')
Child.dad('')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Grandmother: # non class-incest parent class
def grandmother(self): # define function
print('Mary')
class Grandfather: # non class-incest sub parent class
def grandfather(self): # define function
print('Jerry')
class Mom(Grandfather): # single class-incest child
def mom(self): # define function
print('Jane')
class Dad(Mom,Grandfather,Grandmother): # triple class-incest child
def dad(self): # define function
print('John')
class Child(Dad,Mom,Grandfather,Grandmother):pass # Oh, you poor Child class act. lol
class Child(Dad,Mom,Grandmother,Grandfather):pass # MRO: Method Resolution Order error
Child.grandmother('')
Child.grandfather('')
Child.mom('')
Child.dad('')
# I am almost a complete Walking Human Computer Science Research Laboratory Machine on Two Legs... 😁