Last active
July 3, 2018 16:49
-
-
Save wasi0013/c5e8378cf7eb327db6da5f832e410bdf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import ABC, abstractmethod | |
class Polygon(ABC): | |
@abstractmethod | |
def area(self): | |
pass | |
@abstractmethod | |
def perimeter(self): | |
pass | |
class Triangle(Polygon): | |
def __init__(self, a, b, c, base, height): | |
self.a, self.b, self.c = a, b, c | |
self.base, self.height = base, height | |
def area(self): | |
return 1/2 * self.base * self.height | |
def perimeter(self): | |
return self.a + self.b + self.c | |
class Square(Polygon): | |
def __init__(self, a): | |
self.a, = a | |
def area(self): | |
return self.a * self.a | |
def perimeter(self): | |
return 2 * self.a | |
obj = Triangle(a=1, b=2, c=3, base=2, height=3) | |
print(obj.area()) | |
# The following line will raise | |
# TypeError: Can't instantiate abstract class Polygon with abstract methods area | |
# obj = Polygon() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment