๐
Abstract class in Python
When we want to avoid creating an instance from a class, we define the class as an abstract because this class is base for another class and we don't want to create an instance from this class.
We implement the method of the base class as an abstract method because this method is common among all child class and each child class want to custom implementation of the method.
python
from abc import abstractmethod, ABCclass InvalidOperationError(Exception): # new exceptionpassclass Stream(ABC):def __init__(self):self.streamopen = Falsedef open(self):if self.streamopen:raise InvalidOperationError("The File is already open")self.streamopen = Truedef close(self):if not self.streamopen:raise InvalidOperationError("The File is already close")self.streamopen = False@abstractmethoddef read(self):passclass FileStream(Stream):def read(self):print("reading from File")class NetworkStream(Stream):def read(self):print("reading from Network")