Skip to content

Instantly share code, notes, and snippets.

@wasi0013
Last active July 3, 2018 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wasi0013/c5e8378cf7eb327db6da5f832e410bdf to your computer and use it in GitHub Desktop.
Save wasi0013/c5e8378cf7eb327db6da5f832e410bdf to your computer and use it in GitHub Desktop.
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