Advertisement
c0d3dsk1lls

super() function in python CodedSkills.net

Aug 3rd, 2022
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. # super() = FUNCTION USED TO GIVE ACCESS TO THE METHODS OF A PARENT CLASS.
  2. # RETURNS A TEMPORARY OBJECT OF A PARENT CLASS WHEN USED
  3.  
  4. #---------------------------------------------------------
  5. class Rectangle:
  6.     def __init__(self, length, width):
  7.         self.length = length
  8.         self.width = width
  9. #----------------------------------------------------------
  10.  
  11. class Square(Rectangle):
  12.     def __init__(self, length, width):
  13.         super().__init__(length, width)
  14.     def area(self):
  15.         return self.length*self.width
  16.  
  17. #        self.length = length
  18. #        self.width = width
  19. #--------------------------------------
  20.  
  21. class Cube(Rectangle):
  22.     def __init__(self, length, width, height):
  23.         super().__init__(length, width)
  24.         self.height = height
  25.     def volume(self):
  26.         return self.length*self.width*self.height
  27. #        self.length = length
  28. #        self.width = width
  29. square = Square(3, 3)
  30. cube = Cube(3, 3, 3)
  31. print(square.area())
  32. print(cube.volume())
  33. #----------------------------------------
  34.  
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement