-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperMultipleInheritance.py
More file actions
67 lines (49 loc) · 1.71 KB
/
Copy pathSuperMultipleInheritance.py
File metadata and controls
67 lines (49 loc) · 1.71 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
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * self.length + 2 * self.width
class Square(Rectangle):
def __init__(self, length):
super(Square, self).__init__(length, length)
# calling .area_2() will give us an AttributeError since .base and .height don’t have any values.
# To fix this it requires to use **kwargs, which is very messy.
# https://realpython.com/python-super/#what-can-super-do-for-you
# class Triangle:
# def __init__(self, base, height):
# self.base = base
# self.height = height
# def tri_area(self):
# return 0.5 * self.base * self.height
# class RightPyramid(Square, Triangle):
# def __init__(self, base, slant_height):
# self.base = base
# self.slant_height = slant_height
# super().__init__(self.base)
# def area(self):
# base_area = super().area()
# perimeter = super().perimeter()
# return 0.5 * perimeter * self.slant_height + base_area
# def area_2(self):
# base_area = super().area()
# triangle_area = super().tri_area()
# return triangle_area * 4 + base_area
# pyramid = RightPyramid(2, 4)
# print(pyramid.area_2())
class VolumeMixin:
def volume(self):
return self.area() * self.height
class Cube(VolumeMixin, Square):
def __init__(self, length):
super().__init__(length)
self.height = length
def face_area(self):
return super().area()
def surface_area(self):
return super().area() * 6
cube = Cube(2)
print(cube.surface_area())
print(cube.volume())