-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChapter 47 - SuperFunction.py
More file actions
50 lines (34 loc) · 1.14 KB
/
Chapter 47 - SuperFunction.py
File metadata and controls
50 lines (34 loc) · 1.14 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
# CHAPTER 47
# super() = function used to give access to the methods of a parent class
# returns a temporary object of a parent class when used
class Rectangle:
# Constructor method or __init__ method in Python
def __init__(self, length, width):
self.length = length
self.width = width
class Square(Rectangle):
def __init__(self, length, width):
# self.length = length
# self.height = height
# This is repeated and in programming,
# we don't want that
# so use the super()
super().__init__(length, width)
def area(self):
return self.length * self.width
class Cube(Rectangle):
def __init__(self, length, width, height):
# self.length = length
# self.height = height
# This is repeated and in programming,
# we don't want that
# so use the super()
super().__init__(length, width)
self.height = height
def volume(self):
return self.width * self.length * self.height
square = Square(3, 3)
cube = Cube(3, 3, 3)
# Testing if the super() works
print(square.area())
print(cube.volume())