|
| 1 | +# Play around with this class inheritance scheme |
| 2 | +# Python program example. |
| 3 | + |
| 4 | +class Math: # parent class Math: |
| 5 | + |
| 6 | + global a,b # use global variables inside classes and functions alike: |
| 7 | + a,b=6,2 # use a multivariable, since it's too small to be a tuple or a list. |
| 8 | + |
| 9 | + def addition(num1,num2): |
| 10 | + return num1+num2 |
| 11 | + |
| 12 | + def subtraction(num1,num2): |
| 13 | + return num1-num2 |
| 14 | + |
| 15 | + def multiplication(num1,num2): |
| 16 | + return num1*num2 |
| 17 | + |
| 18 | + def exponent(num1,num2): |
| 19 | + return num1**num2 |
| 20 | + |
| 21 | + def division(num1,num2): |
| 22 | + return num1/num2 |
| 23 | + |
| 24 | +class People: # parent class People: |
| 25 | + |
| 26 | + global names # use global variables inside classes and functions alike: |
| 27 | + names=( |
| 28 | + ['Galileo','Galilei'], # use a 2d list: |
| 29 | + ['Isaac','Newton'], |
| 30 | + ['Albert','Einstein'], |
| 31 | + ['Stephen','Hawking']) |
| 32 | + |
| 33 | + def name(fname,lname): |
| 34 | + return f'{fname} {lname} loves Physics.' # Tip: use the f' format for easier string concatenation. |
| 35 | + |
| 36 | +class Both(Math,People): # child class Both with Math and People classes: |
| 37 | + pass |
| 38 | + |
| 39 | +# Look very carefully at these class inheritance schemes, |
| 40 | +# you notice the class names Math, People and Both. Each |
| 41 | +# of these following examples visually show how class |
| 42 | +# inheritance works. The class Both inherits the all the |
| 43 | +# properties of the Math class and the People class. |
| 44 | + |
| 45 | +print(Math.addition(a,b)) |
| 46 | +print(Math.subtraction(a,b)) |
| 47 | +print(Math.multiplication(a,b)) |
| 48 | +print(Math.exponent(a,b)) |
| 49 | +print(Math.division(a,b)) |
| 50 | + |
| 51 | +print(People.name(names[0][0],names[0][1])) |
| 52 | +print(People.name(names[1][0],names[1][1])) |
| 53 | +print(People.name(names[2][0],names[2][1])) |
| 54 | +print(People.name(names[3][0],names[3][1])) |
| 55 | + |
| 56 | +print(Both.addition(a,b)) |
| 57 | +print(Both.subtraction(a,b)) |
| 58 | +print(Both.multiplication(a,b)) |
| 59 | +print(Both.exponent(a,b)) |
| 60 | +print(Both.division(a,b)) |
| 61 | + |
| 62 | +print(Both.name(names[0][0],names[0][1])) |
| 63 | +print(Both.name(names[1][0],names[1][1])) |
| 64 | +print(Both.name(names[2][0],names[2][1])) |
| 65 | +print(Both.name(names[3][0],names[3][1])) |
| 66 | + |
| 67 | +# Instead, why not shorten your code in the 'print()' function, |
| 68 | +# using strings. |
| 69 | + |
| 70 | +num1=Math.addition(a,b) |
| 71 | +name1=People.name(names[0][0],names[0][1]) |
| 72 | + |
| 73 | +num2=Both.addition(a,b) |
| 74 | +name2=Both.name(names[0][0],names[0][1]) |
| 75 | + |
| 76 | +print(num1) |
| 77 | +print(name1) |
| 78 | + |
| 79 | +print(num2) |
| 80 | +print(name2) |
| 81 | + |
| 82 | +# If you have lots of code in your classes, you can create |
| 83 | +# a tuple or a list for them to shorten the code inside the |
| 84 | +# 'print()' function. You can also notice how each tuple |
| 85 | +# example has its own parent and child class attributes. |
| 86 | + |
| 87 | +math1=( |
| 88 | + Math.addition(a,b), |
| 89 | + Math.subtraction(a,b), |
| 90 | + Math.multiplication(a,b), |
| 91 | + Math.exponent(a,b), |
| 92 | + Math.division(a,b)) |
| 93 | + |
| 94 | +math2=( |
| 95 | + Both.addition(a,b), |
| 96 | + Both.subtraction(a,b), |
| 97 | + Both.multiplication(a,b), |
| 98 | + Both.exponent(a,b), |
| 99 | + Both.division(a,b)) |
| 100 | + |
| 101 | +print(math1[0]) |
| 102 | +print(math2[0]) |
| 103 | + |
| 104 | +names1=( |
| 105 | + People.name(names[0][0],names[0][1]), |
| 106 | + People.name(names[1][0],names[1][1]), |
| 107 | + People.name(names[2][0],names[2][1]), |
| 108 | + People.name(names[3][0],names[3][1])) |
| 109 | + |
| 110 | +names2=( |
| 111 | + Both.name(names[0][0],names[0][1]), |
| 112 | + Both.name(names[1][0],names[1][1]), |
| 113 | + Both.name(names[2][0],names[2][1]), |
| 114 | + Both.name(names[3][0],names[3][1])) |
| 115 | + |
| 116 | +print(names1[0]) |
| 117 | +print(names2[0]) |
| 118 | + |
| 119 | +math_and_people1=( |
| 120 | + Math.addition(a,b), |
| 121 | + Math.subtraction(a,b), |
| 122 | + Math.multiplication(a,b), |
| 123 | + Math.exponent(a,b), |
| 124 | + Math.division(a,b), |
| 125 | + People.name(names[0][0],names[0][1]), |
| 126 | + People.name(names[1][0],names[1][1]), |
| 127 | + People.name(names[2][0],names[2][1]), |
| 128 | + People.name(names[3][0],names[3][1])) |
| 129 | + |
| 130 | +for i in math_and_people1: |
| 131 | + print(i) |
| 132 | + |
| 133 | +# Let's use the class name variable 'Both' and combine |
| 134 | +# all our function calls inside a single tuple, then we will |
| 135 | +# create a for-loop to call them all. |
| 136 | + |
| 137 | +math_and_people2=( |
| 138 | + Both.addition(a,b), |
| 139 | + Both.subtraction(a,b), |
| 140 | + Both.multiplication(a,b), |
| 141 | + Both.exponent(a,b), |
| 142 | + Both.division(a,b), |
| 143 | + Both.name(names[0][0],names[0][1]), |
| 144 | + Both.name(names[1][0],names[1][1]), |
| 145 | + Both.name(names[2][0],names[2][1]), |
| 146 | + Both.name(names[3][0],names[3][1])) |
| 147 | + |
| 148 | +for i in math_and_people2: |
| 149 | + print(i) |
0 commit comments