1# It's kinda hard to explain this just by code.
2# So I'll provide a link to a pretty good explanation of it.
3https://www.pythonforbeginners.com/super/working-python-super-function
1# what's super() function for, might help you a little
2
3class parent:
4 def __init__(self):
5 self.A = 'A'
6 self.B = 'B'
7 def get(self):
8 return self.A
9
10class child1(parent):
11 def __init__(self):
12 super().__init__()
13 self.C = 'C'
14 # def get(self):
15 # return self.A
16
17class child2(parent):
18 def __init__(self):
19 super().__init__()
20 self.C = 'C'
21 def get(self):
22 # return self.A
23 return self.C
24
25class child3(parent):
26 def __init__(self):
27 super().__init__()
28 self.C = 'C'
29 def get(self):
30 # return self.A
31 T = super().get()
32 return T + self.C
33
34class child4(parent):
35 def __init__(self):
36 # super().__init__()
37 self.C = 'C'
38 def get(self):
39 # return self.A
40 return self.C
41
42class child5(parent):
43 def __init__(self):
44 # super().__init__()
45 self.C = 'C'
46 # def get(self):
47 # return self.A
48
49pa = parent()
50c1 = child1()
51c2 = child2()
52c3 = child3()
53c4 = child4()
54c5 = child5()
55
56print(pa.get()) # 'A'
57
58# we use get func from taken from parent class
59print(c1.get()) # 'A'
60
61# get function has been overrides
62print(c2.get()) # 'C'
63
64# calling get function from parent then do whatever we want
65print(c3.get()) # 'AC'
66
67# get function has been overrides, same like child4
68print(c4.get()) # 'C'
69
70# throw an exception, get function from parent returning var A
71# but child5 don't have it
72try:
73 # AttributeError: 'child5' object has no attribute 'A'
74 print(c5.get())
75except AttributeError as err:
76 print('AttributeError:', err)
77
78# check this:
79print(parent.get) # function parent.get
80print(child1.get) # function parent.get
81print(child2.get) # function child2.get
82print(child3.get) # function child3.get
83print(child4.get) # function child4.get
84print(child5.get) # function parent.get
85
86###############################################################################
87# a little example
88
89class point:
90 def __init__(self, x, y):
91 self.x = x
92 self.y = y
93
94 # you might want know about this things:
95 # __add__, __sub__, __mul__, __truediv__, ...
96 def __add__(self, value):
97 return self.x + value, self.y + value
98
99class point3D(point):
100 def __init__(self, x, y, z):
101 super().__init__(x, y)
102 self.z = z
103
104 def __add__(self, value):
105 return *super().__add__(value), self.z + value
106 # equivalent to:
107 # t = super().__add__(value)
108 # return t[0], t[1], self.z + value
109
110A = point(10, 20)
111B = point3D(100, 200, 300)
112
113print(A) # __main__.point object
114print(B) # __main__.point3D object
115
116print(A + 2) # (12, 22)
117print(B + 2) # (102, 202, 302)
118
119print(type(A)) # class '__main__.point'
120print(type(B)) # class '__main__.point3D'
121
122print(type(A) == point) # True
123print(type(B) == point) # False
124
125print(type(A) == point3D) # False
126print(type(B) == point3D) # True
127
128print(isinstance(A, point)) # True
129print(isinstance(B, point)) # True
130
131print(isinstance(A, point3D)) # False
132print(isinstance(B, point3D)) # True
133
1class Square(Rectangle):
2 def __init__(self, length):
3 super().__init__(length, length)
1# The super() function lets you run a parent class function inside the child class.
2class Parent(object):
3 def __init__(self, age):
4 self.age = age
5
6 def func(self):
7 print(f"Hi, my age is {self.age}!")
8
9class Child(Parent):
10 def __init__(self, age):
11 # Here is where I can use the super to run the parent class __init__ function to set the childs' name
12 super().__init__(age)
13
14dad = Parent(36)
15kid = Child(8)
16
17dad.func()
18kid.func() # The kid inherits it from the dad, so I could run it like that too
1class Person:
2 name = ""
3
4 def __init__(self, personName):
5 self.name = personName
6
7 def showName(self):
8 print(self.name)
9
10class Student(Person):
11 studentClass = ""
12
13 def __init__(self, studentName, studentClass):
14 Super().__init__(self, studentName)
15 self.studentClass = studentClass
16
17 def getStudentClass(self):
18 return self.studentClass
19
20
21person1 = Person("Dave")
22person1.showName() # Dave
23student1 = Student("Mary", "Maths")
24print(student1.getStudentClass()) # Math
25student1.showName() # Mary
1class Rectangle:
2 def __init__(self, length, width):
3 self.length = length
4 self.width = width
5
6 def area(self):
7 return self.length * self.width
8
9 def perimeter(self):
10 return 2 * self.length + 2 * self.width
11
12# Here we declare that the Square class inherits from the Rectangle class
13class Square(Rectangle):
14 def __init__(self, length):
15 super().__init__(length, length)
16