Repository navigation
Expand file tree
/
Copy pathEasy Class Python Program Examples Two.py
More file actions
519 lines (357 loc) · 20.5 KB
/
Copy pathEasy Class Python Program Examples Two.py
File metadata and controls
519 lines (357 loc) · 20.5 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# Easy Class Python Program Examples Two:
# 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)
# Let's create docstrings for our easy class Python program examples.
# We need to invoke three double quote marks """ on each side
# of the docstring title: """Easy class""". The getattr() function
# simply means the word: 'get attribute'. With docstrings, we can
# see the documentation payload/description block of the class
# at execution/run time.
# 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:
# Let's learn what 'documentation strings' do with classes. In Python,
# we call these 'doctrings'. We need to invoke three double quote
# marks """ on each side of the docstring title: """Easy class""".
# The getattr() function simply means the word: 'get attribute'. With
# docstrings, we can see the documentation payload/description
# block of the class at execution/run time. Docstrings are like a
# comment; they descibe what the class is about at execution/run
# time.
class Easy_class:
"""Easy class""" # documentation payload/description block
def __init__(self,attribute): # two parameters
self.attribute = attribute # one explicit attribute
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
# Below is our 'instance', along with its argument value: 'Hello'
# and its attribute name: 'attribute'
print(Easy_class('Hello').attribute) # Hello
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# We can also create 'implicit attributes', where the actual value
# 'Hello' is set as an attribute property. We only need to add an
# 'argument placeholder value' in our instance, along with its
# attribute name, called 'attribute'. We can name attributes any
# names we wish. However, in our lesson on easy class Python
# program examples, we will use the attribute name as 'attribute',
# like we did in our Easy Class Python Program Examples: volume
# one.
class Easy_class:
"""Easy class""" # documentation payload/description block
def __init__(self,attribute): # two parameters
self.attribute = 'Hello' # one implicit attribute
print(getattr(Easy_class,'__doc__')) # Easy class
print(Easy_class('argument placeholder value').attribute) # Hello
# or this:
print(Easy_class('').attribute) # Hello
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
"""Easy class""" # documentation payload/description block
def __init__(self,attribute1,attribute2): # three parameters
self.attribute1 = attribute1
self.attribute2 = attribute2 # two explicit attributes
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
# Below is our 'instances', along with their two argument values: 'Hello'
# and 'World!' with their attribute names: 'attribute1' and attribute2.
print(Easy_class('Hello','World!').attribute1) # Hello
print(Easy_class('Hello','World!').attribute2) # World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# This is the implicit version of the same class example above.
class Easy_class:
"""Easy class""" # documentation payload/description block
def __init__(self,attribute1,attribute2): # three parameters
self.attribute1 = 'Hello'
self.attribute2 = 'World!' # two implicit attributes
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
# Below is our 'instances', along with their two argument values: 'Hello'
# and 'World!' with their attribute names: 'attribute1' and attribute2.
print(Easy_class('argument placeholder value','argument placeholder value').attribute1) # Hello
print(Easy_class('argument placeholder value','argument placeholder value').attribute2) # World!
# or this:
print(Easy_class('','').attribute1) # Hello
print(Easy_class('','').attribute2) # World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Now, let's get out of this downpour of redundant code and get
# lazy and DRY: (Don't Repeat Yourself). Let's do the same class
# example above, but this time, we will create a for loop with only
# one print() function. The for loop will increment the two instance
# attributes into one print() function alone. We need to also create
# a tuple named: 'easy_class_values' to store our two attributes,
# attribute1 and attribute2. Let's also invoke the end=' ' suffix to
# keep our text output on one line only.
class Easy_class:
"""Easy class""" # documentation payload/description block
def __init__(self,attribute1,attribute2): # three parameters
self.attribute1 = attribute1
self.attribute2 = attribute2 # two explicit attributes
easy_class_values = (
Easy_class('Hello','World!').attribute1,
Easy_class('Hello','World!').attribute2)
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
for i in easy_class_values:
print(i,end=' ') # Hello World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# This is the implicit version of the same class example above.
class Easy_class:
"""Easy class""" # documentation payload/description block
def __init__(self,attribute1,attribute2): # three parameters
self.attribute1 = 'Hello'
self.attribute2 = 'World!' # two implicit attributes
easy_class_values = (
Easy_class('argument placeholder value','argument placeholder value').attribute1,
Easy_class('argument placeholder value','argument placeholder value').attribute2)
# or this:
easy_class_values = (
Easy_class('','').attribute1,
Easy_class('','').attribute2)
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
for i in easy_class_values:
print(i,end=' ') # Hello World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's type a small paragraph with docstrings only. We will create
# an empty class this time, without attribute properties to illustrate
# commented output about the class object description on the
# screen
class Easy_class:
"""Docstrings are so much fun.
They are so easy to create.""" # documentation payload/description block
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's learn what __slots__ do! Slots reserve exact memory
# for class attributes. You don't need a huge, empty warehouse
# just to store a few attributes. Slots optimize memory and performance.
# Research Python __slots__ to learn more about them. Two double
# underscores __ __ are required to create __slots__. Simply
# press and hold down the shift key, while pressing the minus key.
# Note: when invoking __slots__ with only one attribute, you must
# place a comma ',' at the end of the slot's single attribute, as not
# to confuse it as a tuple value at execution/run time.
class Easy_class:
"""Easy class""" # documentation payload/description block
__slots__ = ('attribute',) # __slots__ optimizes memory and performance
def __init__(self,attribute): # two parameters
self.attribute = attribute # one explicit attribute
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
# Below is our 'instance', along with its argument value: 'Hello'
# and its attribute name: 'attribute'
print(Easy_class('Hello').attribute) # Hello
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
"""Easy class""" # documentation payload/description block
__slots__ = ('attribute',) # __slots__ optimizes memory and performance
def __init__(self,attribute): # two parameters
self.attribute = 'Hello' # one implicit attribute
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
# Below is our 'instance', along with its argument value: 'Hello'
# and its attribute name: 'attribute'
print(Easy_class('argument placeholder value').attribute) # Hello
# or this:
print(Easy_class('').attribute) # Hello
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
"""Easy class""" # documentation payload/description block
__slots__ = ('attribute1','attribute2') # __slots__ optimizes memory and performance
def __init__(self,attribute1,attribute2): # three parameters
self.attribute1 = attribute1
self.attribute2 = attribute2 # two explicit attributes
easy_class_values = (
Easy_class('Hello','World!').attribute1,
Easy_class('Hello','World!').attribute2)
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
for i in easy_class_values:
print(i,end=' ') # Hello World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# This is the implicit version of the same class example above.
class Easy_class:
"""Easy class""" # documentation payload/description block
__slots__ = ('attribute1','attribute2') # __slots__ optimizes memory and performance
def __init__(self,attribute1,attribute2): # three parameters
self.attribute1 = 'Hello'
self.attribute2 = 'World!' # two implicit attributes
easy_class_values = (
Easy_class('argument placeholder value','argument placeholder value').attribute1,
Easy_class('argument placeholder value','argument placeholder value').attribute2)
# or this:
easy_class_values = (
Easy_class('','').attribute1,
Easy_class('','').attribute2)
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
for i in easy_class_values:
print(i,end=' ') # Hello World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's learn what *args and **kwargs do. These are easy, once
# you learn them. We will take it step by step, like we did before.
# Invoke the asterisk * in front of the *args attribute. Note: *args
# can be any name you wish. However, the Python standard is
# invoked as *args for its name, which is short for the name
# 'arguments'.
# With *args, we don't have to worry about how many parameters
# we need to satisfy how many arguments we need as we had to
# before. *args are great when there are far too many arguments
# needed to satisfy the exact number of parameter values.
class Easy_class:
"""Easy class""" # documentation payload/description block
__slots__ = ('args',) # __slots__ optimizes memory and performance
def __init__(self,*args): # two parameters
self.args = args # one explicit attribute
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
print(Easy_class('Hello','World!','Computer','Science.').args[0]) # Hello
print(Easy_class('Hello','World!','Computer','Science.').args[1]) # World!
print(Easy_class('Hello','World!','Computer','Science.').args[2]) # Computer
print(Easy_class('Hello','World!','Computer','Science.').args[3]) # Science.
# or this:
easy_class_values = Easy_class('Hello','World!','Computer','Science.')
# Invoke the try and except IndexError handler to catch index range
# errors, without crashing this Python program example.
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
try:
for i in range(5):
print(easy_class_values.args[i],end=' ') # Hello World! Computer Science.
except IndexError:pass
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's see what **kwargs do. Invoke the double asterisk ** in
# front of the **kwargs attribute. Note: **kwargs can be any name
# you wish. However, the Python standard is invoked as **kwargs
# for its name, which is short for the name 'keyword_arguments'.
# With **kwargs, we don't have to worry about how many parameters
# we need to satisfy how many keyword arguments we need as we
# had to before. **kwargs are great when there are far too many keyword
# arguments needed to satisfy the exact number of parameter values.
class Easy_class:
"""Easy class""" # documentation payload/description block
__slots__ = ('kwargs',) # __slots__ optimizes memory and performance
def __init__(self,**kwargs):
self.kwargs = kwargs
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
# Invoke the try and except KeyError handler to catch keyword
# argument errors and keyword argument index errors without
# crashing this Python program example.
try:
print(Easy_class(kwargs = 'Hello').kwargs['kwargs1'])
except KeyError:
print('Attribute not found:')
# or this:
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
try:
print(Easy_class(kwargs = 'Hello').kwargs['kwargs1'])
except KeyError:pass
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's invoke the .get() function so we don't have to use an
# error handler, which also creates less code for us to write.
class Easy_class:
"""Easy class""" # documentation payload/description block
__slots__ = ('kwargs',) # __slots__ optimizes memory and performance
def __init__(self,**kwargs):
self.kwargs = kwargs
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
# Notice how keyword arguments also reside within the instance?
# Easy_class(kwargs = 'Hello').kwargs.get('kwargs')
print(Easy_class(kwargs = 'Hello').kwargs.get('kwargs'))
# or this:
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
print(Easy_class(kwargs = 'Hello').kwargs.get('kwargs','Attribute not found:')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
"""Easy class""" # documentation payload/description block
__slots__ = ('kwargs',) # __slots__ optimizes memory and performance
def __init__(self,**kwargs):
self.kwargs = kwargs
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
print(
Easy_class(
kwargs1 = 'Hello',
kwargs2 = 'World!').kwargs.get('kwargs1','Attribute Not Found:')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
"""Easy class""" # documentation payload/description block
__slots__ = ('kwargs',) # __slots__ optimizes memory and performance
def __init__(self,**kwargs):
self.kwargs = kwargs
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
print(
Easy_class(
kwargs1 = 'Hello',
kwargs2 = 'World!',
kwargs3 = 'Computer',
kwargs4 = 'Science.').kwargs.get('kwargs4','Attribute Not Found:')) # optional'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# BONUS LESSON FUN!
# Why use bulky try and except error handlers, when we can simply
# invoke the getattr() 'get attribute' function to make less lines of code,
# unlike a try and except error handler that requires more lines of code.
# You must add the attribute 'Fallback Attribute' as a FALLBACK, or you
# will get an attribute error. that will crash the program, execution/run.
# The getattr() 'get attribute' function takes place of the bulky try and
# except error handler block with far less Python code.
# print(getattr(Easy_class('Hello'),'attribute','Fallback Attribute'))
class Easy_class:
"""Easy class""" # documentation payload/description block
__slots__ = ('attribute',) # __slots__ optimizes memory and performance
def __init__(self,attribute): # two parameters
self.attribute = attribute # one explicit attribute
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
print(getattr(Easy_class('Hello'),'attribute','Fallback Attribute')) # Hello
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
"""Easy class""" # documentation payload/description block
__slots__ = ('attribute1','attribute2') # __slots__ optimizes memory and performance
def __init__(self,attribute1,attribute2): # three parameters
self.attribute1 = attribute1
self.attribute2 = attribute2 # two explicit attributes
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
# Below is our 'instances', along with their two argument values: 'Hello'
# and 'World!' with their attribute names: 'attribute1' and attribute2.
print(getattr(Easy_class('Hello','World!'),'attribute1','Sorry! Attribute not found:')) # Hello
print(getattr(Easy_class('Hello','World!'),'attribute2','Sorry! Attribute not found:')) # World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# This is the implicit version of the same class example above.
class Easy_class:
"""Easy class""" # documentation payload/description block
__slots__ = ('attribute1','attribute2') # __slots__ optimizes memory and performance
def __init__(self,attribute1,attribute2): # three parameters
self.attribute1 = 'Hello'
self.attribute2 = 'World!' # two implicit attributes
print(getattr(Easy_class,'__doc__')) # docstring: Easy class
# Below is our 'instances', along with their two argument values: 'Hello'
# and 'World!' with their attribute names: 'attribute1' and attribute2.
print(getattr(Easy_class('',''),'attribute1','Sorry! Attribute not found:')) # Hello
print(getattr(Easy_class('',''),'attribute2','Sorry! Attribute not found:')) # World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Well, that's all folks! We are at the end of this grueling Python
# programming lesson on Python classes. I sure hope I've cleared
# the fog and set the path for the novice/beginner to follow...
# To those who have learned Python through me. I may
# not speak in any of my Python manuals. I write better
# than I can speak, when it comes to explaining anything.
# However, I strive to bring others with me all the way, or
# we don't go at all! I'm no teacher by a longshot, but I
# will make sure what I learn through others about
# Python and computers in general. I will always make
# learning fun and easy, no matter the complexity of
# what I solve. I want to pass onward the torch of those
# who had taught me, when they once passed the torch
# onto me...
# I now pass onward the torch of which, I had learned to
# light, so I could light the way for others abound.
# Yours truly, Joe
# And please note: my grammar might not ever be the best
# and brightest. However, I make darn sure that my Python
# programing skills are solid steel. Nonetheless...
# I am almost a complete Walking Human Computer Science Research Laboratory Machine on Two Legs... 😁