-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefine Functions and Class Objects.py
More file actions
1235 lines (905 loc) · 47.1 KB
/
Define Functions and Class Objects.py
File metadata and controls
1235 lines (905 loc) · 47.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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Let's learn some deep Python programming skills with define functions
# and class objects. These Python programming examples are not for the
# beginner/novice Python programmer. To those, who are starting into
# computer programming, do not start off with these Python program
# examples. You will quickly get lost trying to understand such examples
# as these. These Python programming examples are for advanced
# programmers only. However, if you are brave enough to try these Python
# programming examples, I say. Why not!! But you might become lost
# and quickly confused if you are just starting fresh into computer
# programming at large...
# Most of these Python programming examples will go into both robots.
# I have a Robomaster S1 robot and a brand new Robomaster EP Core
# robot. Both are from dji. The EP Core robot is coming in the not too
# distant future from now. So be looking out for it.
# Created by Joseph C. Richardson, GitHub.com
# Create a simple define function along with the print() function.
def define_function_example_one():
print('This is a basic define function() example.')
define_function_example_one()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Create a define function with one parameter variable along with
# the print() function.
def define_function_example_two(argument):
print('This is a basic define function() with an argument example.')
define_function_example_two('argument placeholder value')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Create a define function with two parameter variables along with
# the print() function.
def define_function_example_three(argument1,argument2):
print('This is a basic define function() with two arguments example.')
define_function_example_three('argument placeholder value','argument placeholder value')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# When you are not sure of how many parameter variables to use
# within a define function, you can invoke the *args prefix to satisfy
# any number of argument placeholder values without the worry of
# how many are needed to satisfy the parameter variables.
def define_function_Class_example_four(*args):
print('This is a basic define function() with an *args example.')
define_function_Class_example_four('argument placeholder value','argument placeholder value')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Create a return define function that returns a value, via invoking one
# parameter variable, along with one argument placeholder value.
def define_function_Class_example_five(argument):
return 'I am the actual returned value.'
print(define_function_Class_example_five(
'This is a return define function() with an argument placeholder value.'))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Create a return define function that returns two values, via invoking two
# parameter variables, along with two argument placeholder values.
def define_function_example_six(argument1,argument2):
return 'I am the actual returned value one.','I am the actual returned value two.'
print(define_function_example_six(
'This is a return define function() with an argument example.',
'This is a return define function() with an argument example.')[1])
# or this:
def define_function_example_six(argument1,argument2):
return (
'I am the actual returned value one.',
'I am the actual returned value two.')
print(define_function_example_six(
'This is a return define function() with an argument example.',
'This is a return define function() with an argument example.')[1])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# When you are not sure of how many parameter variables to use
# within a return define function, you can invoke the *args prefix to
# satisfy any number of argument placeholder values without the
# worry of how many are needed to satisfy the parameter variables.
def define_function_example_seven(*args):
return 'I am the actual returned value one.','I am the actual returned value two.'
print(define_function_example_seven('This is a return define function() with an argument example.')[1])
# or this:
def define_function_example_seven(*args):
return (
'I am the actual returned value one.',
'I am the actual returned value two.')
print(define_function_example_seven(
'This is a return define function() with an argument example.')[1])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's learn what **kwargs are all about. The word 'kwargs' simply means
# the words 'keyword arguments' for short. Two asterisks ** are used for
# **kwargs. Use **kwargs when you don't know how many keyword argument
# variables you want within your define function parameters. Note: you do
# not need to call the word '**kwargs' as kwargs. However, you will need to
# invoke two asterisks ** to make **kwargs work. Programmers know the word
# as **kwargs by standard definition, but you can use your own words.
def keyword_arguments(**kwargs): # one keyword argument variable
print('I am the actual value.')
keyword_arguments(
keyword1='keyword argument placeholder value1',
keyword2='keyword argument placeholder value2') # two keyword argument values
# This example is without any **kwargs at all; we have to
# satisfy the exact number of keyword arguments to the
# exact number of keyword argument placeholder values.
def keyword_arguments(keyword_arg1,keyword_arg2): # two keyword argument variables
print('I am the actual value.')
keyword_arguments(
keyword_arg1='keyword argument placeholder value1',
keyword_arg2='keyword argument placeholder value2') # two keyword argument values
# As shown in the define function() example above, how we
# needed the exact number of keyword argument values to the
# exact number of keyword argument variables. However, with
# **kwargs you no longer have to worry about how many keyword
# argument values you will need to satisfy the number of keyword
# argument variables within the define function parameters.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# define functions can also return keyword arguments. In this
# first example, the exact number of keyword arguments are
# required to satisfy the exact number of keyword argument
# placeholder values within the print() function.
def keyword_arguments(kwarg1,kwarg2):
return 'I am the actual returned value one.','I am the actual returned value two.',
print(keyword_arguments(kwarg1='placeholder value 1',kwarg2='placeholder value 2'))
# define functions can also return keyword arguments. In this
# second example, the exact number of keyword arguments are
# not required to satisfy the exact number of keyword argument
# placeholder values within the print() function.
def keyword_arguments(**kwargs):
return 'I am the actual returned value.'
print(keyword_arguments(kwarg1='placeholder value 1',kwarg2='placeholder value 2'))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# define functions can also return keyword arguments. In this
# first example, the exact number of keyword arguments are
# required to satisfy the exact number of keyword argument
# placeholder values within the print() function.
def keyword_arguments(num1,num2):
return num1*num2
print(keyword_arguments(num1=2,num2=4))
# define functions can also return keyword arguments. In this
# second example, the exact number of keyword arguments are
# not required to satisfy the exact number of keyword argument
# placeholder values within the print() function.
def keyword_arguments(**kwargs):
return 2*4
print(keyword_arguments(num1='placeholder value 1',num2='placeholder value 2'))
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's do a quick recap about *args and **kwargs syntex only,
# so we can fully understand in a simpler way of what *args and
# **kwargs are all about.
def arguments(*args): # one argument variable
print('I am the actual value.')
arguments(
'argument placeholder value1',
'argument placeholder value2') # two argument values
def keyword_arguments(**kwargs): # one keyword argument variable
print('I am the actual value.')
keyword_arguments(
kwargs1='keyword argument placeholder value1',
kwargs2='keyword argument placeholder value1') # two keyword argument values
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create some define functions that act like subroutines.
# Since there are no line numbers in Python, also means that we
# cannot create any such 'go to', or 'go sub' commands at all with
# Python. So how can we create subroutines with Python?. How
# can we create subroutines without making them jump to line
# numbers, like we did in the old days? Well the answer is quite
# simple. Let's use define functions() with a while loop to create
# our subroutine examples, using define functions() only.
def subroutine_one():
print('You picked subroutine one:')
def subroutine_two():
print('You picked subroutine two:')
def subroutine_three():
print('You picked subroutine three:')
while True:
message=input('Please type 1, 2 or 3 to select the subroutine you wish to \
display or type (q) to quit: ').lower().strip() # strip() clears whitespace)
if message=='q':
break
while True:
if message=='1':
subroutine_one()
break
elif message=='2':
subroutine_two()
break
elif message=='3':
subroutine_three()
break
else:
print('Sorry! No subroutine for that.')
break
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Global Variables in Python
# What are 'global variables'? Global variables are used within
# define functions() when you want to call a variable outside the
# actual define function(), you need the prefix 'global' to be able
# to call a variable outside the actual define function() itself. For
# example here is what calling a variable, using the prefix 'global'
# does.
def global_variable():
global a
a = 'global variables work outside their define functions()'
global_variable()
print(a)
# If you try to call a variable outside a define function() with no
# 'global' prefix attached to it, you will get an error such as this:
def non_global_variable():
b = 'non global variables won\'t work outside their define functions()'
non_global_variable()
print(b)
# Traceback (most recent call last):
# File "C:\Users\mogie54321\Downloads\Define Functions and Class Objects.py", line 257, in <module>
# print(b)
# NameError: name 'b' is not defined
# You can, however do this if you don't want to invoke the 'global' prefix. So
# instead, we can do this:
b = 'outside variables work outside their define functions()'
def non_global_variable():
print(b)
non_global_variable()
# Simply create a variable outside the define function and then
# call it in from outside it. Let's do more global variables to get
# the hang of things with a group of them.
def global_variables():
global a
global b
global c
a = 'global variable (a) works outside its define function()'
b = 'global variable (b) works outside its define function()'
c = 'global variable (c) works outside its define function()'
global_variables()
print(a)
# Here is one thing you should avoid doing. If you call an outside
# variable with the same name as this example below shows, you
# won't get the desired output you want. If you want outside variables
# outside of global variables, you must give them different names so
# they can know who is who.
a = 'oops! This outside variable (a) was overwritten by the global variable (a).'
def global_variables():
global a
global b
global c
a = 'global variable (a) works outside its define functions()'
b = 'global variable (b) works outside its define functions()'
c = 'global variable (c) works outside its define functions()'
global_variables()
print(a)
# Now that we used a different name for the outside variable, the
# global variables and the outside variable will know who is who,
# when being called upon. Note: you can also use an uppercase 'A'
# for the outside variable; computers are case sensitive: 'a' and 'A'
# are not the same to a computer. Therefore, the computer treats
# the variables as totally different names. So please keep this in
# mind at all times.
abc = 'Now I know which one of us variables are being called.'
def global_variables():
global a
global b
global c
a = 'global variable (a) works outside its define functions()'
b = 'global variable (b) works outside its define functions()'
c = 'global variable (c) works outside its define functions()'
global_variables()
print(abc)
print(a)
# The computer thinks this is a totally different variable name altogether.
A = 'I still know which one of us variables are being called.'
def global_variables():
global a
global b
global c
a = 'global variable (a) works outside its define functions()'
b = 'global variable (b) works outside its define functions()'
c = 'global variable (c) works outside its define functions()'
global_variables()
print(A)
print(a)
# Let's pack global variables with just one 'global' prefix to use
# them outside the define function().
def global_variables():
global a,b,c
a = 'pack global variable (a) works outside its define functions()'
b = 'pack global variable (b) works outside its define functions()'
c = 'pack global variable (c) works outside its define functions()'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's learn what class objects are all about with simple class attribute
# properties, so we can start to have an understanding of what class
# object attribute property values are all about, as well as how many
# argument placeholder values each class object needs to satisfy for
# their argument values and visa versa.
class Class_attribute_properties_example_one:
def __init__(self,var):
self.class_var=var
a = Class_attribute_properties_example_one(
'This is an instance of a single class object with one attribute property.').class_var
print(a)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Class_attribute_properties_example_two:
def __init__(self,var1,var2):
self.class_var1=var1
self.class_var2=var2
b = Class_attribute_properties_example_two(
'This is an instance of a single class object with two attribute properties.',
'This is an instance of a single class object with two attribute properties.').class_var2
print(b)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Class_attribute_properties_example_three:
def __init__(self,var1,var2,var3):
self.class_var1=var1
self.class_var2=var2
self.class_var3=var3
c = Class_attribute_properties_example_three(
'This is an instance of a single class object with three attribute properties.',
'This is an instance of a single class object with three attribute properties.',
'This is an instance of a single class object with three attribute properties.').class_var3
print(c)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class Class_attribute_properties_example_four:
def __init__(self,var1,var2,var3,var4):
self.class_var1=var1
self.class_var2=var2
self.class_var3=var3
self.class_var4=var4
d = Class_attribute_properties_example_four(
'This is an instance of a single class object with four attribute properties.',
'This is an instance of a single class object with four attribute properties.',
'This is an instance of a single class object with four attribute properties.',
'This is an instance of a single class object with four attribute properties.').class_var4
print(d)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Create a class with three parameter variables, including the 'self'
# variable. Create three print() functions outside the class that will
# display the three class values on the screen output, when executed.
class Class_example_one:
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print(Class_example_one('John','Smith',23).first_name)
print(Class_example_one('John','Smith',23).last_name)
print(Class_example_one('John','Smith',23).age)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Create a class with three parameter variables, including the 'self'
# variable. Create three print() functions inside the class that will
# display the three class values on the screen output, when executed.
# be sure to invoke the 'self' variable within the three print() functions.
class Class_example_two:
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print(self.first_name)
print(self.last_name)
print(self.age)
Class_example_two('John','Smith',23)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# This is the very same class example as the one above. The only difference
# is, that a separate return function is also created within the class object by
# itself, inside a print() function, called 'pets'.
class Class_example_three:
def pets(pet1,pet2,pet3,pet4):
return 'Dog','Cat','Bird','Fish'
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print(self.first_name)
print(self.last_name)
print(self.age)
Class_example_three('John','Smith',23)
print(Class_example_three.pets(
'argument placeholder value',
'argument placeholder value',
'argument placeholder value',
'argument placeholder value')[2])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# This is the very same class example as the one above. The only
# difference is, that the *args prefix is invoked. When you are not
# sure of how many parameter variables to use within a return define
# function, you can invoke the *args prefix to satisfy any number of
# argument placeholder values without the worry of how many are
# needed to satisfy the parameter variables.
class Class_example_four:
def pets(*args):
return 'Dog','Cat','Bird','Fish'
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print(self.first_name)
print(self.last_name)
print(self.age)
Class_example_four('John','Smith',23)
print(Class_example_four.pets('argument placeholder value')[2])
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# This is the very same class example as the one above. The only
# difference is, a variable is used to store the class name and the
# return function name, 'pets'.
class Class_example_five:
def pets(*args):
return 'Dog','Cat','Bird','Fish'
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print(self.first_name)
print(self.last_name)
print(self.age)
Class_example_five('John','Smith',23)
return_function = Class_example_five.pets('argument placeholder value')[2]
print(return_function)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's, this time create a Main class and a Sub class so we can
# inherit all attribute properties from the main class to the subclass.
class Main_class_example_one:
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print(self.first_name)
print(self.last_name)
print(self.age)
class Sub_class_example_one(Main_class_example_one): # inherit all attribute properties from the main class
pass
Main_class_example_one('John','Smith',23)
Sub_class_example_one('Jane','Smith',22)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Here is a small example of the 'super()' function to show how
# it works. The 'super()' function not only inherits all attribute
# properties from the main class, but it also carries its very own
# sub class attribute properties as well. This way, we can create
# different sub class attribute properties as you will clearly notice
# in the next class example.
class Main_class_example_two:
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print(self.first_name)
print(self.last_name)
print(self.age)
class Sub_class_example_two(Main_class_example_two): # inherit all attribute properties from the main class
def __init__(self,fname,lname,age):
super().__init__(fname,lname,age)
Main_class_example_two('John','Smith',23)
Sub_class_example_two('Jane','Smith',22)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# This sub class act has the exact, but redundant attribute properties
# that are the very same attribute properties within the Main class act.
class Main_class_attributes:
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
class Sub_class_attributes:
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print(Main_class_attributes('Jane','Smith',22).first_name)
print(Sub_class_attributes('John','Smith',23).first_name)
# Let's fix this redundant attribute property problem, once and for all
# with the super() function.
class Main_class_attributes:
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
class Sub_class_attributes(Main_class_attributes):
def __init__(self,fname,lname,age):
super().__init__(fname,lname,age)
print(Main_class_attributes('Jane','Smith',22).first_name)
print(Sub_class_attributes('John','Smith',23).first_name)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Now we can reach a much better understanding of what the super()
# function does. We can create a sub class with its very own atribute
# properties, without having to create redundancy on our programming
# part. We have to invoke all the main class atribute properties into the
# super() function, along with the sub class attribute properties. But we
# don't have to recreate main class atribute property redundancy at all.
class Main_class_example_three:
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print(self.first_name)
print(self.last_name)
print(self.age)
# Inherit all attribute properties from the main class.
class Sub_class_example_three(Main_class_example_three):
# inherit all attribute properties from the main class to the sub class, without
# redundancy while, only creating the sub class attribute properties of their
# very own set of atribute properties only. In this example, we are creating
# four pet attribute properties. Note: You cannot call sub class atrubes, which
# are different to those that are in the main class, unless you use the super()
# function. Always invoke the __init__ constructor, which initalizes all the
# atribute properites from the main class, while we can add some new class
# atribute properties to the sub class act, without recreating all the attribute
# properties of the main class act.
def __init__(self,fname,lname,age,dog,cat,bird,fish):
super().__init__(fname,lname,age)
self.dog=dog
self.cat=cat
self.bird=bird
self.fish=fish
print(self.dog)
print(self.cat)
print(self.bird)
print(self.fish)
Main_class_example_three('John','Smith',23)
Sub_class_example_three('Jane','Smith',22,'German Shepherd','Tabby Cat','Parrot','Angelfish')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# This is the very same main class Python program example as
# shown above. The only difference is, the print() functions are
# all outside from the two classes: main class and the sub class
# acts.
class Main_class_example_four:
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
class Sub_class_example_four(Main_class_example_four):
def __init__(self,fname,lname,age,dog,cat,bird,fish):
super().__init__(fname,lname,age)
self.dog=dog
self.cat=cat
self.bird=bird
self.fish=fish
# All print() functions are outside from the main class and the sub
# class acts.
print(Main_class_example_four('John','Smith',23).first_name)
print(Main_class_example_four('John','Smith',23).last_name)
print(Main_class_example_four('John','Smith',23).age)
print(Sub_class_example_four('Jane','Smith',22,'German Shepherd','Tabby Cat','Parrot','Angelfish').first_name)
print(Sub_class_example_four('Jane','Smith',22,'German Shepherd','Tabby Cat','Parrot','Angelfish').last_name)
print(Sub_class_example_four('Jane','Smith',22,'German Shepherd','Tabby Cat','Parrot','Angelfish').age)
print(Sub_class_example_four('Jane','Smith',22,'German Shepherd','Tabby Cat','Parrot','Angelfish').dog)
print(Sub_class_example_four('Jane','Smith',22,'German Shepherd','Tabby Cat','Parrot','Angelfish').cat)
print(Sub_class_example_four('Jane','Smith',22,'German Shepherd','Tabby Cat','Parrot','Angelfish').bird)
print(Sub_class_example_four('Jane','Smith',22,'German Shepherd','Tabby Cat','Parrot','Angelfish').fish)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's now take all that we've learned so far and start to have some
# serious Python programming fun! Let's create some text strings
# to make the Python program be a piece of programming art, as
# well as taking a huge step into computer science in general.
# Believe it or not!
class Main_class_example_five:
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
# Keep all the print() functions inside the main class.
print('Hi. My first name is',self.first_name+'. What is your first name?')
print('My last name is',self.last_name+'. What is your last name?')
print('My age is',str(self.age)+'. How old are you?')
class Sub_class_example_five(Main_class_example_five):
def __init__(self,fname,lname,age,dog,cat,bird,fish):
super().__init__(fname,lname,age)
self.dog=dog
self.cat=cat
self.bird=bird
self.fish=fish
# Keep all the print() functions inside the sub class.
print('I have a',self.dog)
print('I have a',self.cat)
print('I have a',self.bird)
print('I have a',self.fish)
# Now, simply call up the two class names alone, along with
# their argument placeholder values, without invoking the
# print() function outside the classes as we did before in the
# last example, shown above; thus creating longer Python
# command code on the programmer's part.
Main_class_example_five('John','Smith',23)
Sub_class_example_five('Jane','Smith',22,'German Shepherd','Tabby Cat','Parrot','Angelfish')
# or this:
# Break up all Python commands so they won't disappear off
# the left edge of the screen.
Main_class_example_five('John','Smith',23)
Sub_class_example_five(
'Jane','Smith',22,'German Shepherd',
'Tabby Cat','Parrot','Angelfish')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's ratchet things up a bit and take all we've learned about
# classes and return functions, along with learning what the
# super() function does, along with its __init__ constructor,
# which initializes all the attribute properties from the main
# class to the sub class, without having to recreate main class
# attribute properties, when we want the sub class to have its
# very own, unique set of attribute properties, while being able
# to inherit the main class attribute properties without redundancy
# on the programmer's part.
class Main_class_example_six:
def number_one_example(num1,num2):
return num1+num2
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print('Hi. My first name is',self.first_name+'. What is your first name?')
print('My last name is',self.last_name+'. What is your last name?')
print('My age is',str(self.age)+'. How old are you?')
class Sub_class_example_six(Main_class_example_six):
def number_two_example(num1,num2):
return num1*num2
def __init__(self,fname,lname,age,dog,cat,bird,fish):
super().__init__(fname,lname,age)
self.dog=dog
self.cat=cat
self.bird=bird
self.fish=fish
print('I have a',self.dog)
print('I have a',self.cat)
print('I have a',self.bird)
print('I have a',self.fish)
# Call the 'main_class_example_six' variable, followed by its
# return function, called 'number_one_example'.
print(Main_class_example_six.number_one_example(2,5))
# Call the 'main_class_example_six' variable, followed by its
# return function, called 'number_two_example'.
print(Sub_class_example_six.number_two_example(2,5))
# Lastly, call the two classes, the main class and the sub class
# acts, along their own, unique argument placeholder values.
Main_class_example_six('John','Smith',23)
Sub_class_example_six('Jane','Smith',22,'German Shepherd','Tabby Cat','Parrot','Angelfish')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# This is the very same Python program example as the one above.
# The only difference is, this time we have all the print() functions
# inside the two classes. All we need to do is call the two classes
# without having to call the separate return functions outside the
# classes as we did before.
class Main_class_example_six:
def number_one_example(num1,num2):
return num1+num2
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print('Hi. My first name is',self.first_name+'. What is your first name?')
print('My last name is',self.last_name+'. What is your last name?')
print('My age is',str(self.age)+'. How old are you?')
class Sub_class_example_six(Main_class_example_six):
def number_two_example(num1,num2):
return num1*num2
def __init__(self,fname,lname,age,dog,cat,bird,fish):
super().__init__(fname,lname,age)
self.dog=dog
self.cat=cat
self.bird=bird
self.fish=fish
print('I have a',self.dog)
print('I have a',self.cat)
print('I have a',self.bird)
print('I have a',self.fish)
print(Main_class_example_six.number_one_example(2,5))
print(Sub_class_example_six.number_two_example(2,5))
Main_class_example_six('John','Smith',23)
Sub_class_example_six('Jane','Smith',22,'German Shepherd','Tabby Cat','Parrot','Angelfish')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# This is the very same Python program example as the one above.
# Let's just imagine that the super() function does not exist at all.
# Imagine the redundancy of the attribute properties of the main
# class to the sub class? The sub class has redundant Python code
# related to the main class which, without the super() function, we
# have to repeat this bit of Python code illustrated below:
# self.first_name=fname
# self.last_name=lname
# self.age=age
class Main_class_example_six:
def number_one_example(num1,num2):
return num1+num2
def __init__(self,fname,lname,age):
self.first_name=fname
self.last_name=lname
self.age=age
print('Hi. My first name is',self.first_name+'. What is your first name?')
print('My last name is',self.last_name+'. What is your last name?')
print('My age is',str(self.age)+'. How old are you?')
class Sub_class_example_six(Main_class_example_six):
def number_two_example(num1,num2):
return num1*num2
def __init__(self,fname,lname,age,dog,cat,bird,fish):
# redundant Python code from the main class attribute properties:
self.first_name=fname
self.last_name=lname
self.age=age
# sub class attribute properties
self.dog=dog
self.cat=cat
self.bird=bird
self.fish=fish
print('I have a',self.dog)
print('I have a',self.cat)
print('I have a',self.bird)
print('I have a',self.fish)
print(Main_class_example_six.number_one_example(2,5))
print(Sub_class_example_six.number_two_example(2,5))
Main_class_example_six('John','Smith',23)
Sub_class_example_six('Jane','Smith',22,'German Shepherd','Tabby Cat','Parrot','Angelfish')
# Now you know why the super() function is invoked into classes,
# who's sub classes have different attribute properties of their very
# own. The super() function stops upper class attribute property
# redundancy on the programmer's part.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Bonus Python String Concatenation Tips:
# Create a tuple called 'tuple_varianble', so we can use its values in
# text trings.
tuple_variable = 'Dog','Cat','Bird','Fish'
# Non formatted string concatenation example:
print('I love my',tuple_variable[0],'and my',tuple_variable[1],'so very much!')
print('I love my '+tuple_variable[0]+' and my '+tuple_variable[1]+' so very much!')
# old formatted string concatenation example:
print('I love my {} and my {} so very much!'.format(tuple_variable[0],tuple_variable[1]))
# New f' string concatenation format:
print(f'I love my {tuple_variable[0]} and my {tuple_variable[1]} so very much!')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# When it comes to computer programming, text strings and
# integer strings do not mix at all. To tackle this string concatenation
# problem, we invoke the 'str()' string function, so we can mix
# them both together with ease, while doing real calculations
# with integer strings, within text strings.
# Non formatted string concatenation example:
print('Invoke the str() function to concatenate text and integer strings together:',3+4,'just like that.')
print('Invoke the str() function to concatenate text and integer strings together: '+str(3+4)+' just like that.')
# New f' string concatenation format:
print(f'Invoke the str() function to concatenate text and integer strings together: {str(3+4)} just like that.')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Invoke the ( \ ) backslash in print() function text to force a hard
# line break to make Python print() function text code be on multiple
# lines.
print('This is a paragraph inside a print() function, using the \
backslash to create a hard line break. We can keep on typing \
as many lines of text we need inside a print() function.')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Invoke the ( \n ) backslash 'n' to force a line break in the printed
# screen output text on multiple lines.
print('This is a paragraph inside a print() function, using the \
backslash to create a hard line break.\nWe can keep on typing \
as many lines of text we need inside a print() function.\nWe can \
force printed screen output text on multiple lines.')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's try another way to create printed text paragraphs, without
# invoking the ( \ ) backslash and the ( \n ) backslash 'n'.
# Let's use three single ( ''' ''' ) quote marks instead.
print('''This is a paragraph inside a print() function, using the
backslash to create a hard line break. We can keep on typing
as many lines of text we need inside a print() function. We can
force printed screen output text on multiple lines.''')
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
# Let's create a variable to store the above print() function text code.
text_string_variable = 'This is a paragraph inside a print() function, \
using the backslash to create a hard line break.\nWe can keep on typing \
as many lines of text we need inside a print() function.\nWe can \
force printed screen output text on multiple lines.'
print(text_string_variable) # prints out the value inside the variable called 'text'
# or this without the ( \ ) backslash and the ( \n ) backslash 'n'