Advertisement
chegehus

Area_perimeter

Feb 22nd, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. from abc import ABC, abstractmethod
  2. class Shape(ABC): #superclass  also Shape class is inheriting from ABC module
  3.     @abstractmethod #decorator,it makes the methods below them abstract
  4.     def area():pass
  5.    
  6.     @abstractmethod #decorator   and we dont want the user to create an instance of shape class
  7.     def perimeter(self):pass
  8.    
  9.    
  10. class Square(Shape):#subclass
  11.     def __init__(self, side):
  12.         self.__side = side
  13.     def area(self):
  14.         return self._side * self._side
  15.     def perimeter(self):
  16.         return self._side *4
  17.        
  18. shape = Square(5)
  19. print(shape.area())
  20. print(shape.perimeter())
  21. OUTPUTS:
  22. AttributeError                            Traceback (most recent call last)
  23. <ipython-input-35-5ca981efe69b> in <module>
  24.      17
  25.      18 shape = Square(5)
  26. ---> 19 print(shape.area())
  27.      20 print(shape.perimeter())
  28.      21
  29.  
  30. <ipython-input-35-5ca981efe69b> in area(self)
  31.      12         self.__side = side
  32.      13     def area(self):
  33. ---> 14         return self._side * self._side
  34.      15     def perimeter(self):
  35.      16         return self._side *4
  36.  
  37. AttributeError: 'Square' object has no attribute '_side'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement