shapes.py
https://www.geeksforgeeks.org/abstract-classes-in-python/
They had a great document on what abstract class is.
#!/usr/bin/env python3 ''' DOC: A polygon is a plane figure that is described by a finite number of straight line segments connected to form a closed polygonal chain or polygonal circuit. The solid plane region, the bounding circuit, or the two together, may be called a polygon.\n A polygon is a family of shapes: Triangle, Square, Pentagon, etc. They are all part of the family of polygons. https://www.geeksforgeeks.org/abstract-classes-in-python/ An abstract class can be considered as a blueprint for other classes. It allows you to create a set of methods that must be created within any child classes built from the abstract class. A class which contains one or more abstract methods is called an abstract class. An abstract method is a method that has a declaration but does not have an implementation. While we are designing large functional units we use an abstract class. When we want to provide a common interface for different implementations of a component, we use an abstract class. Why use Abstract Base Classes : By defining an abstract base class, you can define a common Application Program Interface(API) for a set of subclasses. This capability is especially useful in situations where a third-party is going to provide implementations, such as with plugins, but can also help you when working in a large team or with a large code-base where keeping all classes in your mind is difficult or not possible. How Abstract Base classes work : By default, Python does not provide abstract classes. Python comes with a module which provides the base for defining Abstract Base classes(ABC) and that module name is ABC. ABC works by decorating methods of the base class as abstract and then registering concrete classes as implementations of the abstract base. A method becomes abstract when decorated with the keyword @abstractmethod. ''' from abc import ABC class Polygon(ABC): # abstract method def howmanysides(self): pass class Triangle(Polygon): # overriding abstract method def howmanysides(self): print("Triangle: I have 3 sides") class Square(Polygon): # overriding abstract method def howmanysides(self): print("Square I have 4 equal sides with 90 degree right angles") class Pentagon(Polygon): # overriding abstract method def howmanysides(self): print("Pentagon: I have 5 sides") class Hexagon(Polygon): # overriding abstract method def howmanysides(self): print("Hexagon: I have 6 sides") class Quadrilateral(Polygon): # overriding abstract method def howmanysides(self): print("Quadrilateral: I have 4 sides") class Rectangle(Polygon): # overriding abstract method def howmanysides(self): print("Rectangle: I have 4 sides, 2 short and 2 long") # Driver code
main.py
#!/usr/bin/env python3 import shapes from shapes import * def main(): print("{}".format(shapes.__doc__)) A = Triangle() A.howmanysides() A = Square() A.howmanysides() A = Quadrilateral() A.howmanysides() A = Rectangle() A.howmanysides() A = Pentagon() A.howmanysides() A = Hexagon() A.howmanysides() if __name__ == '__main__': main()
Output
DOC: A polygon is a plane figure that is described by a finite number of straight line segments connected to form a closed polygonal chain or polygonal circuit. The solid plane region, the bounding circuit, or the two together, may be called a polygon. A polygon is a family of shapes: Triangle, Square, Pentagon, etc. They are all part of the family of polygons. https://www.geeksforgeeks.org/abstract-classes-in-python/ An abstract class can be considered as a blueprint for other classes. It allows you to create a set of methods that must be created within any child classes built from the abstract class. A class which contains one or more abstract methods is called an abstract class. An abstract method is a method that has a declaration but does not have an implementation. While we are designing large functional units we use an abstract class. When we want to provide a common interface for different implementations of a component, we use an abstract class. Why use Abstract Base Classes : By defining an abstract base class, you can define a common Application Program Interface(API) for a set of subclasses. This capability is especially useful in situations where a third-party is going to provide implementations, such as with plugins, but can also help you when working in a large team or with a large code-base where keeping all classes in your mind is difficult or not possible. How Abstract Base classes work : By default, Python does not provide abstract classes. Python comes with a module which provides the base for defining Abstract Base classes(ABC) and that module name is ABC. ABC works by decorating methods of the base class as abstract and then registering concrete classes as implementations of the abstract base. A method becomes abstract when decorated with the keyword @abstractmethod. Triangle: I have 3 sides Square I have 4 equal sides with 90 degree right angles Quadrilateral: I have 4 sides Rectangle: I have 4 sides, 2 short and 2 long Pentagon: I have 5 sides Hexagon: I have 6 sides Process finished with exit code 0