Repository navigation
Expand file tree
/
Copy pathEasy Class Python Program Examples.py
More file actions
544 lines (378 loc) · 20.9 KB
/
Copy pathEasy Class Python Program Examples.py
File metadata and controls
544 lines (378 loc) · 20.9 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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# Easy Class Python Program Examples:
# 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. This lesson clearly shows
# manual, redundant ways to create Python classes. These
# redundant print() functions show how print() functions work
# in classes, so the novice/beginner can learn to not only create
# classes. But the novice/beginner can also learn why classes
# work in the first place. 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...
# First of all, we must learn the three parts that make up a class.
# The class blueprint or empty template looks like this example:
class Easy_class:
def __init__(self,attribute): # two parameters
self.attribute = attribute # one attribute
# The word 'self' can be any name you wish to use. How-
# ever, programmers use 'self' as the standard. The 'self' word
# is an address in memory that points back to its attribute, so it
# can be accessible to itself. The class constructor __init__
# initiates the attributes of an instance to gain access to arguments
# within an instance of a class. Class constructors can also be
# called dunder methods or magic methods. However, the term
# class constructor is the true name. Two double underscores
# __ __ are required to create class constructors. Simply press
# and hold down the shift key, while pressing the minus key.
# 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)
# 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.
class Easy_class:
def __init__(self,attribute): # two parameters
self.attribute = attribute # one attribute
print(Easy_class('Hello').attribute) # Hello
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's type and execute/run this same Easy_class Python program
# example. This time we will create a variable called easy_class_value
# so we can store the instance in it.
class Easy_class:
def __init__(self,attribute): # two parameters
self.attribute = attribute # one attribute property
print(Easy_class('Hello').attribute) # Hello
# or this:
easy_class_value = Easy_class('Hello') # Create a variable to store the instance.
print(easy_class_value.attribute) # Hello
# or this:
easy_class_value = Easy_class('Hello').attribute # Create a variable to store the instance along with its attribute.
print(easy_class_value) # Hello
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's type and execute/run this Easy_class Python program
# example. This time, we have two attribute properties and two
# arguments, Hello and World! The two print() functions demonstrate
# how two attribute extensions: attribute1 and attribute2 are invoked
# at the end of the two print() functions.
class Easy_class:
def __init__(self,attribute1,attribute2): # three parameters
self.attribute1 = attribute1
self.attribute2 = attribute2 # two attribute properties
print(Easy_class('Hello','World!').attribute1) # Hello
print(Easy_class('Hello','World!').attribute2) # World!
# or this:
easy_class_values = Easy_class('Hello','World!') # Create a variable to store the instance.
# Invoke a + sign or a comma to concatenate strings together.
print(easy_class_values.attribute1+easy_class_values.attribute2) # HelloWorld!
print(easy_class_values.attribute1,easy_class_values.attribute2) # Hello World!
# or this:
easy_class_values = Easy_class('Hello','World!').attribute1,Easy_class('Hello','World!').attribute2
# Invoke a + sign or a comma to concatenate strings together.
print(easy_class_values[0]+easy_class_values[1]) # HelloWorld!
print(easy_class_values[0],easy_class_values[1]) # Hello World!
# or this:
easy_class_values = (
Easy_class('Hello','World!').attribute1,
Easy_class('Hello','World!').attribute2)
print(easy_class_values[0]+easy_class_values[1]) # HelloWorld!
print(easy_class_values[0],easy_class_values[1]) # Hello World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's do this again, but we will use the very last way and the
# shortest way to create code. We will also add two more attributes
# to our very same class examples, as shown above.
class Easy_class:
def __init__(self,attribute1,attribute2,attribute3,attribute4): # five parameters
self.attribute1 = attribute1
self.attribute2 = attribute2
self.attribute3 = attribute3
self.attribute4 = attribute4 # four attribute properties
easy_class_values = (
Easy_class('Hello','World!','Computer','Science.').attribute1,
Easy_class('Hello','World!','Computer','Science.').attribute2,
Easy_class('Hello','World!','Computer','Science.').attribute3,
Easy_class('Hello','World!','Computer','Science.').attribute4)
# Invoke a + sign or a comma to concatenate strings together.
print(
easy_class_values[0]+
easy_class_values[1]+
easy_class_values[2]+
easy_class_values[3]) # HelloWorld!ComputerScience
print(
easy_class_values[0],
easy_class_values[1],
easy_class_values[2],
easy_class_values[3]) # Hello World! Computer Science
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create a for loop that increments through a tuple of values.
class Easy_class:
def __init__(self,attribute1,attribute2,attribute3,attribute4): # five parameters
self.attribute1 = attribute1
self.attribute2 = attribute2
self.attribute3 = attribute3
self.attribute4 = attribute4 # four attribute properties
easy_class_values = (
Easy_class('Hello','World!','Computer','Science.').attribute1,
Easy_class('Hello','World!','Computer','Science.').attribute2,
Easy_class('Hello','World!','Computer','Science.').attribute3,
Easy_class('Hello','World!','Computer','Science.').attribute4)
for i in easy_class_values:
print(i)
# or invoke the end='' suffix to keep text on one line.
for i in easy_class_values:
print(i,end='') # HelloWorld!ComputerScience.
# or this:
for i in easy_class_values:
print(i,end=' ') # Hello World! Computer Science.
# or this:
for i in easy_class_values:
print(i,end=' space ') # Hello space World! space Computer space Science. space
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Stay DRY: Don't Repeat Yourself. Let's create a default tuple
# of values so we don't have to repeat attribute values over and over.
class Easy_class:
def __init__(self,attribute1,attribute2,attribute3,attribute4): # five parameters
self.attribute1 = attribute1
self.attribute2 = attribute2
self.attribute3 = attribute3
self.attribute4 = attribute4 # four attribute properties
arg_placeholder_value = 'Hello','World!','Computer','Science.' # no more repeated attribute values
easy_class_values = (
Easy_class(arg_placeholder_value[0],arg_placeholder_value[1],arg_placeholder_value[2],arg_placeholder_value[3]).attribute1,
Easy_class(arg_placeholder_value[0],arg_placeholder_value[1],arg_placeholder_value[2],arg_placeholder_value[3]).attribute2,
Easy_class(arg_placeholder_value[0],arg_placeholder_value[1],arg_placeholder_value[2],arg_placeholder_value[3]).attribute3,
Easy_class(arg_placeholder_value[0],arg_placeholder_value[1],arg_placeholder_value[2],arg_placeholder_value[3]).attribute4)
print(easy_class_values[0]) # Hello
print(easy_class_values[1]) # World!
print(easy_class_values[2]) # Computer
print(easy_class_values[3]) # Science.
# or this:
for i in easy_class_values:
print(i,end=' ') # Hello World! Computer Science.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create implicit attribute values with our very same class
# example above.
class Easy_class:
def __init__(self,attribute1,attribute2,attribute3,attribute4): # five parameters
self.attribute1 = 'Hello'
self.attribute2 = 'World!'
self.attribute3 = 'Computer'
self.attribute4 = 'Science.' # four attribute properties with four implicit arguments
arg_placeholder_value = '','','','' # no more repeated implicit attribute values
easy_class_values = (
Easy_class(arg_placeholder_value[0],arg_placeholder_value[1],arg_placeholder_value[2],arg_placeholder_value[3]).attribute1,
Easy_class(arg_placeholder_value[0],arg_placeholder_value[1],arg_placeholder_value[2],arg_placeholder_value[3]).attribute2,
Easy_class(arg_placeholder_value[0],arg_placeholder_value[1],arg_placeholder_value[2],arg_placeholder_value[3]).attribute3,
Easy_class(arg_placeholder_value[0],arg_placeholder_value[1],arg_placeholder_value[2],arg_placeholder_value[3]).attribute4)
print(easy_class_values[0]) # Hello
print(easy_class_values[1]) # World!
print(easy_class_values[2]) # Computer
print(easy_class_values[3]) # Science.
# or this:
for i in easy_class_values:
print(i,end=' ') # Hello World! Computer Science.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create four implicit attributes with one, single attribute
# property. Let's also create a for loop to keep our code DRY.
class Easy_class:
def __init__(self,attribute1,attribute2,attribute3,attribute4): # five parameters
self.attribute = 'Hello','World!','Computer','Science.' # one attribute property with four implicit arguments
arg_placeholder_value = '','','','' # no more repeated implicit attribute values
DRY = Easy_class(
arg_placeholder_value[0],
arg_placeholder_value[1],
arg_placeholder_value[2],
arg_placeholder_value[3])
# Invoke the try and except IndexError handler to catch index range
# errors, without crashing this Python program example.
try:
for i in range(5):
print(DRY.attribute[i],end=' ') # Hello World! Computer Science.
except IndexError:
print('\nIndex Range Error:')
# or this:
try:
for i in range(5):
print(DRY.attribute[i],end=' ') # Hello World! Computer Science.
except IndexError:pass
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# 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:
def __init__(self,*args): # two parameters
self.args = args # one attribute
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.
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:
def __init__(self,**kwargs):
self.kwargs = kwargs
# 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','Error! Not to worry...'])
except KeyError:
print('Attribute not found:')
# or this:
try:
print(Easy_class(kwargs = 'Hello').kwargs['kwargs1','Error! Not to worry...'])
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:
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'))
# or this:
print(Easy_class(kwargs = 'Hello').kwargs.get('kwargs','Attribute not found:')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
def __init__(self,**kwargs):
self.kwargs = kwargs
print(
Easy_class(
kwargs1 = 'Hello',
kwargs2 = 'World!').kwargs.get('kwargs1','Attribute Not Found:')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
def __init__(self,**kwargs):
self.kwargs = kwargs
print(
Easy_class(
kwargs1 = 'Hello',
kwargs2 = 'World!',
kwargs3 = 'Computer',
kwargs4 = 'Science.').kwargs.get('kwargs4','Attribute Not Found:')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's learn how to break Python's standard naming convention
# rules. Please note: the boss wouldn't be too happy when breaking
# such naming convention rules in Python syntax. Let's use some
# of our early Python class examples of what not to do in Python
# or any other programming languages. Let's change the word 'self'
# to 'selfie'
class Easy_class:
def __init__(selfie,attribute): # two parameters
selfie.attribute = attribute # one attribute
print(Easy_class('Hello').attribute) # Hello
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
def __init__(selfie,*arguments): # two parameters
selfie.arguments = arguments # one attribute
print(Easy_class('Hello','World!','Computer','Science.').arguments[0]) # Hello
print(Easy_class('Hello','World!','Computer','Science.').arguments[1]) # World!
print(Easy_class('Hello','World!','Computer','Science.').arguments[2]) # Computer
print(Easy_class('Hello','World!','Computer','Science.').arguments[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.
try:
for i in range(5):
print(easy_class_values.arguments[i],end=' ') # Hello World! Computer Science.
except IndexError:pass
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Easy_class:
def __init__(selfie,**keyword_arguments):
selfie.keyword_arguments = keyword_arguments
# Notice how keyword arguments also reside within the instance?
# Easy_class(kwargs = 'Hello').kwargs.get('kwargs')
print(Easy_class(keyword_arguments = 'Hello').keyword_arguments.get('kwargs'))
# or this:
print(Easy_class(keyword_arguments = 'Hello').keyword_arguments.get('kwargs','Attribute not found:')) # optional
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# We can also use different variable names in attribute properties.
# For example:
class Easy_class:
def __init__(self,attribute1,attribute2): # three parameters
self.my_attribute1 = attribute1
self.my_attribute2 = attribute2 # two attribute properties
print(Easy_class('Hello','World!').my_attribute1) # Hello
print(Easy_class('Hello','World!').my_attribute2) # World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create a docstring for our last class act example. 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.
class Easy_class:
"""Easy class""" # documentation payload/description block
def __init__(self,attribute): # two parameters
self.attribute = attribute # one attribute
print(getattr(Easy_class,'__doc__')) # Easy class
print(Easy_class('Hello').attribute) # Hello
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# BONUS LESSON!
# 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.
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 attribute properties
print(getattr(Easy_class,'__doc__')) # Easy class
print(Easy_class('Hello','World!').attribute1) # Hello
print(Easy_class('Hello','World!').attribute2) # World!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# These two Python command lines below are what are called
# 'instances'. The green text, 'Hello' and 'World!' are the actual
# arguments you see on a computer's monitor at execution/run
# time. Any text you see on the screen at execution/run time,
# are the actual arguments every easy class Python program
# illustrated here. All the green text are the actual 'arguments'.
Easy_class('Hello','World!').attribute1 # Hello
Easy_class('Hello','World!').attribute2 # 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...
# Believe me, this last part of our lesson on Easy Classes wasn't
# so easy for me to create, as well as creating comments for
# these last two lessons from docstrings, onward to __slots__.
# I am heading into my ninth year with Python programming, since
# Christmas day, 2017. And it was all because of my Best Friend,
# Brian, who lit the fire inside me to take up computer programming
# again. He introduced me to that little Cozmo Robot, and I haven't
# looked back since that day...
# I am almost a complete Walking Human Computer Science Research Laboratory Machine on Two Legs... 😁