Abstract in python

Nandha Balan
1 min readApr 22, 2021

Abstraction in Python is the process of hiding the real implementation of an application from the user and emphasizing only how to use the application.

An abstract method is also known as an incomplete method where we can only implement it in the children’s classes.

How Abstract Base classes work :

By default, Python does not provide abstract classes. Python comes with a module that 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.

Let us see an example of how the abstract method works,

from abc import *
class Nwinterface(ABC):
@abstractmethod
def connect(self):
pass
@abstractmethod
def disconnect(self):
pass
class Videocard(Nwinterface):
def connect(self):
print(‘connecting videocard’)
def disconnect(self):
print(‘disconnecting videocard’)
class TPU(Nwinterface):
def connect(self):
print(‘connecting TPU’)
def disconnect(self):
print(‘disconnecting TPU’)
a = TPU()
a.connect()
a.disconnect()
connecting TPU
disconnecting TPU

Here the parent method is not properly implemented, but the child class is.

--

--