from abc import ABC, abstractmethod class Shape(ABC): # Shape is an abstract base class @abstractmethod def area(self): pass class Circle(Shape): # Circle is a class that inherits from the abstract Shape class def __init__(self, radius): self.radius = radius def area(self): # As a child of an Abstract class, this child class MUST define # it's own implementation of this abstractmethod return 3.14 * self.radius ** 2 class Square(Shape): # Square is a class that inherits from the abstract Shape class def __init__(self, side): self.side = side def area(self): # As a child of an Abstract class, this child class MUST define # it's own implementation of this abstractmethod return self.side ** 2
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter