Repository navigation
Expand file tree
/
Copy pathEasy Class Python Program Examples Three.py
More file actions
381 lines (242 loc) · 15.1 KB
/
Copy pathEasy Class Python Program Examples Three.py
File metadata and controls
381 lines (242 loc) · 15.1 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
# Easy Class Python Program Examples Three:
# by Joseph C. Richardson,GitHub.com
# These Easy Classes are designed for the novice/beginner.
# These easy class Python program examples show step by step
# ways to create classes. Please note: for those who haven't yet
# studied Easy Class Python Program Examples first; it is highly
# recommended that you do so.
# Here is a constant reminder: a class is an object, and an attribute
# is a class object's feature, or part of a class object. These parts
# of a class object are called 'attribute properties'. The attribute, like
# a feature someone has, such as brown hair, blue eyes; anything
# about their appearance are their attributes. In our first example,
# we only have one attribute property, but we didn't put any features
# into it yet. We need to create an instance for this class, along
# with its attribute's name:
# print(Easy_class('Hello').attribute)
# 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...
# Please note: arguments are nothing more than the values you
# see on a computer's monitor at run time, such as the word 'Hello'
# that prints out onto the screen when you type and execute/run
# this Python program example below:
# Lets create a simple easy class, and create a default tuple of
# different text string values, so we can reuse classes that have
# different text argument values in each instance we create from
# one, single class attribute alone.
arguments = 'Hello','World!','Computer','Science','is Fun!!!'
class Easy_class:
def __init__(self,attribute):
self.attribute = attribute # one explicit attribute property
print(getattr(Easy_class(arguments[0]),'attribute','Sorry! Attribute not found:')) # Hello
print(getattr(Easy_class(arguments[1]),'attribute','Sorry! Attribute not found:')) # World!
print(getattr(Easy_class(arguments[2]),'attribute','Sorry! Attribute not found:')) # Computer
print(getattr(Easy_class(arguments[3]),'attribute','Sorry! Attribute not found:')) # Science
print(getattr(Easy_class(arguments[4]),'attribute','Sorry! Attribute not found:')) # is Fun!!!
# Let's keep it DRY! ( Don't Repeat Yourself! )
# Let's do the same class example as above, but we will create a
# for loop that will help us do away with redundant print() functions,
# while our Python code is short and sweet. Nice and DRY.
arguments = 'Hello','World!','Computer','Science','is Fun!!!' # default tuple
class Easy_class:
def __init__(self,attribute):
self.attribute = attribute # one explicit attribute property
for i in arguments:
print(getattr(Easy_class(i),'attribute','Sorry! Attribute not found:'))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# There are two types of classes that do not require try and except
# error handlers. Explicit and implicit attributes do not require try
# and except error handlers. Keyword arguments do not require try
# and except error handlers as well. Keyword arguments require
# the dictionary's .get() method. However, we can use bulky error
# handlers all you want. But why use them, when we can completely
# do without them?
class Easy_class:
def __init__(self,attribute):
self.attribute = attribute # one explicit attribute property
print(getattr(Easy_class('Hello'),'attribute','Sorry! Attribute not found:')) # Hello
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
def __init__(self,attribute):
self.attribute = 'Hello' # one implicit attribute property
print(getattr(Easy_class('argument placeholder value'),'attribute','Sorry! Attribute not found:')) # Hello
# or this:
print(getattr(Easy_class(''),'attribut','Sorry! Attribute not found:')) # optional: text for None
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
def __init__(self,attribute1,attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2 # two explicit attribute properties
print(getattr(Easy_class('Hello','World!'),'attribute1','Sorry! Attribute not found:')) # Hello
print(getattr(Easy_class('Hello','World!'),'attribute2','Sorry! Attribute not found:')) # World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
def __init__(self,attribute1,attribute2):
self.attribute1 = 'Hello'
self.attribute2 = 'World!' # two implicit attribute properties
print(getattr(Easy_class(
'argument placeholder value',
'argument placeholder value'),'attribute1','Sorry! Attribute not found:')) # Hello
print(getattr(Easy_class(
'argument placeholder value',
'argument placeholder value'),'attribute2','Sorry! Attribute not found:')) # World!
# or this:
print(getattr(Easy_class('',''),'attribute','Sorry! Attribute not found:')) # optional: text for None
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
def __init__(self,**kwargs):
self.kwargs = kwargs
# Notice how keyword arguments also reside within the instance?
# Easy_class(kwargs = 'Hello').kwargs.get('kwargs')
print(Easy_class(kwargs = 'Hello').kwargs.get('kwargs')) # Hello
# or this:
print(Easy_class(kwargs = 'Hello').kwargs.get('kwarg','Sorry! Attribute not found:')) # optional: text for None
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
def __init__(self,**kwargs):
self.kwargs = kwargs
# Notice how keyword arguments also reside within the instance?
# Easy_class(kwargs = 'Hello','World!').kwargs.get('kwargs')
print(
Easy_class(
kwargs1 = 'Hello',
kwargs2 = 'World!').kwargs.get('kwargs1')) # Hello
print(
Easy_class(
kwargs1 = 'Hello',
kwargs2 = 'World!').kwargs.get('kwargs2')) # World!
# or this:
print(
Easy_class(
kwargs1 = 'Hello',
kwargs2 = 'World!').kwargs.get('kwarg','Sorry! Attribute not found:')) # optional: text for None
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create sentences out of an instance of our easy class
# Python program example:
arguments = 'Hello','World!','Computer','Science','is Fun!!!' # default tuple
class Easy_class:
def __init__(self,attribute):
self.attribute = attribute # one explicit attribute property
print(getattr(Easy_class(arguments[0]+' everyone!'),'attribute','Sorry! Attribute not found:'))
print(getattr(Easy_class(arguments[0]+' '+arguments[1]+' How are you?'),'attribute','Sorry! Attribute not found:'))
print(getattr(Easy_class(arguments[0]+' '+arguments[1]+' How are you?'),'attribute','Sorry! Attribute not found:'),'More text.')
print(getattr(Easy_class(arguments[0]+' '+arguments[1]+' How are you?'),'attribute','Sorry! Attribute not found:')+' More text.')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create a class with an instance variable that will keep our
# print() function lean, clean and short.
arguments = ('Hello','World!','Computer','Science','is Fun!!!') # default tuple
class Easy_class:
def __init__(self,attribute):
self.attribute = attribute # one explicit attribute property
instance = Easy_class(arguments[0]+' '+arguments[1]+' How are you?')
print(getattr(instance,'attribute','Sorry! Attribute not found:'))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# We can create one attribute property with as many implicit attributes
# as we wish.
class Easy_class:
def __init__(self,attribute):
self.attribute = 'Hello','World!','Computer','Science' # one attribute property with four implicit attributes
print(getattr(Easy_class(''),'attribute')[0])
print(getattr(Easy_class(''),'attribute')[1])
print(getattr(Easy_class(''),'attribute')[2])
print(getattr(Easy_class(''),'attribute')[3])
# or this:
for i in range(4):
print(getattr(Easy_class(''),'attribute')[i])
# or this:
for i in range(4):
print(getattr(Easy_class(''),'attribute')[i],end=' ')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# BONUS LESSON!
# Let's learn what the super() function is all about. The super()
# function allows us to create a brand new class, without having
# to create redundant attribute properties. Here is a skeletal outline
# of the super() function Python program example:
class Easy_class_main:
def __init__(self,attribute1,attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2 # two explicit attribute properties
class Easy_class_super(Easy_class_main):
def __init__(self,attribute1,attribute2):
super().__init__(attribute1,attribute2)
print(getattr(Easy_class_main('Hello','World!'),'attribute1','Sorry! Attribute not found:')) # Hello
print(getattr(Easy_class_super('Hello','World!'),'attribute2','Sorry! Attribute not found:')) # World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# With the super() function, we can add attributes without having
# to rewrite old, redundant attributes over and over.
class Easy_class_main:
def __init__(self,attribute1,attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2 # two explicit attribute properties
class Easy_class_super(Easy_class_main):
def __init__(self,attribute1,attribute2,attribute3): # add the new explicit attribute property
super().__init__(attribute1,attribute2)
self.attribute3 = attribute3 # add an explicit attribute property
print(getattr(Easy_class_main('Hello','World!'),'attribute1','Sorry! Attribute not found:')) # Hello
print(getattr(Easy_class_main('Hello','World!'),'attribute2','Sorry! Attribute not found:')) # World!
print(getattr(Easy_class_super('Hello','World!','Fun!!'),'attribute3','Sorry! Attribute not found:')) # Fun!!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class_main:
def __init__(self,attribute1,attribute2):
self.attribute1 = 'Hello'
self.attribute2 = 'World!' # two implicit attribute properties
class Easy_class_super(Easy_class_main):
def __init__(self,attribute1,attribute2):
super().__init__(attribute1,attribute2)
print(getattr(Easy_class_main('',''),'attribute1','Sorry! Attribute not found:')) # Hello
print(getattr(Easy_class_super('',''),'attribute2','Sorry! Attribute not found:')) # World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# With the super() function, we can add attributes without having
# to rewrite old, redundant attributes over and over.
class Easy_class_main:
def __init__(self,attribute1,attribute2):
self.attribute1 = 'Hello'
self.attribute2 = 'World!' # two implicit attribute properties
class Easy_class_super(Easy_class_main):
def __init__(self,attribute1,attribute2,attribute3): # add an implicit attribute property
super().__init__(attribute1,attribute2)
self.attribute3 = 'Fun!!' # add an implicit attribute property
print(getattr(Easy_class_main('',''),'attribute1','Sorry! Attribute not found:')) # Hello
print(getattr(Easy_class_main('',''),'attribute2','Sorry! Attribute not found:')) # World!
print(getattr(Easy_class_super('','',''),'attribute3','Sorry! Attribute not found:')) # Fun!!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class_main:
def __init__(self,attribute1,attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2 # two explicit attribute properties
class Easy_class_super(Easy_class_main):
def __init__(self,attribute1,attribute2,attribute3,attribute4): # add two new explicit attribute properties
super().__init__(attribute1,attribute2)
self.attribute3 = attribute3 # add an explicit attribute property
self.attribute4 = attribute4 # add an explicit attribute property
print(getattr(Easy_class_main('Hello','World!'),'attribute1','Sorry! Attribute not found:')) # Hello
print(getattr(Easy_class_main('Hello','World!'),'attribute2','Sorry! Attribute not found:')) # World!
print(getattr(Easy_class_super('Hello','World!','Fun!!','Computers'),'attribute3','Sorry! Attribute not found:')) # Fun!!
print(getattr(Easy_class_super('Hello','World!','Fun!!','Computers'),'attribute4','Sorry! Attribute not found:')) # Computers
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class_main:
def __init__(self,attribute1,attribute2):
self.attribute1 = 'Hello'
self.attribute2 = 'World!' # two implicit attribute properties
class Easy_class_super(Easy_class_main):
def __init__(self,attribute1,attribute2,attribute3,attribute4): # add two new implicit attribute properties
super().__init__(attribute1,attribute2)
self.attribute3 = 'Fun!!' # add an implicit attribute property
self.attribute4 = 'Computers' # add an implicit attribute property
print(getattr(Easy_class_main('',''),'attribute1','Sorry! Attribute not found:')) # Hello
print(getattr(Easy_class_main('',''),'attribute2','Sorry! Attribute not found:')) # World!
print(getattr(Easy_class_super('','','',''),'attribute3','Sorry! Attribute not found:')) # Fun!!
print(getattr(Easy_class_super('','','',''),'attribute4','Sorry! Attribute not found:')) # Computers
# The super() function can be as complex as you please. However,
# you should research: Method Resolution Order (MRO) first.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Whoever learns Python through me. You'd better supersede me!!
# A true teacher doesn't hold back their knowledge and keep students
# beneath them. By doing such, they aren't teaching anything at all.
# If I can make those, who learn from me become a brighter light than
# myself, I have done my job as a true teacher, even if I'm not a real,
# licensed teacher of any sort.
# As a true teacher, I will bring you all the way with me. Or we won't go at all...
# I am almost a complete Walking Human Computer Science Research Laboratory Machine on Two Legs... 😁