abstraction in Python:

m umar
Artificial Intelligences Technology
3 min readFeb 13, 2021

Abstraction in python is the concept of object oriented programming, where user is unaware the complex implementation detail of functionality and internal working, whereas user is only able to see the basic functionalities and internal details are hidden.

For example if we take the example of a car, To drive the car it is not compulsory for driver to know the internal working of engine i.e. how engine works, he must only know the basic working i.e. how to use steering ,breaks etc.

The whole dashboard of car is example of abstraction, we do not know the internal working as a driver.

Let’s understand with coding example:

abstraction in python is achieved by abstract classes and interfaces.

Abstract Class:

An abstract class is a class which contains one or more abstract method. An abstract method is a method that has declaration but does not have implementation. It is left to child classes to provide the implementation of abstract methods. If the implementation of the abstract method is not defined in the derived classes, then the Python interpreter throws an error. Abstract class behave as blueprint for other classes.

  • an abstract class can have both normal method and abstract methods
  • abstract classes cannot be instantiated i.e. we cannot create objects from abstract classes.

Read more about abstract classes here.

Interface:

interface provide the method name without method body. It is left for child classes to provide the method body, Here remember one thing that python does not provide interface explicitly we have to use abstract classes to create interface manually. In python you can create interface using abstract class i.e. if an abstract class contains only abstract methods that class will act as interface in python.

Lets consider an example.

To declare the abstract class we need to import abc module by using import keyword.

# use abc module to use abstract class
from abc import ABC,abstractmethod
class AbstractClass(ABC): #abstract class
def TestMethod(self,x):
print("Passed value is = ",x)
def Display(self):
print("we are in abstract class method")
class ChildClass1(AbstractClass): #child 1 class inherited from abstract class
def Display(self):
print("we are in child1 class method")
class ChildClass2(AbstractClass): #child 2 class inherited from abstract class
def Display(self):
print("we are in child2 class method")
# creating objects of child 1 class
obj=ChildClass1()
obj.Display() # we are in child1 class method
# creating objects of child 2 class
obj=ChildClass2()
obj.Display() # we are in child2 class method
obj.TestMethod(4) # Passed value is = 4

Explanation:

First we created abstract class then we create two child classes and inherits the abstract class. Read more about inheritance here.

we have define a method “Display” in every class and at the end we have created object and calling ,ponder on calling and its output when we call display method from child1 class then only child1 class’s display method will execute, same for child 2 class. we can also access abstract class own methods.

why we need abstraction in python?

as in abstraction internal implementation details are hidden to user so user feel easy to use the application. If we show all internal detail to user then normal user(Non technical person) may be confused even he did not get how to use that application so

To ease the use of application and to enhance the efficiency of application we need to hide the irrelevant data to user, that is why we use abstraction.

Polymorphism in python:

Inheritance in python:

Encapsulation in python:

--

--

m umar
Artificial Intelligences Technology
0 Followers

i am python expert and machine learning engineer do follow me if you want to learn python, machine learning and deep learning.