Simplified O.O.P : Abstraction in Python
Tags: python, oop, beginners, webdev
Published: 2022-08-31 18:45:14

##Introduction

In previous post , [Simplified: Object-oriented Programming Python](https://dev.to/titusnjuguna/simplified-object-oriented-programming-python-3l12), I promised to get into details on abstraction. The assumption for this post is you are proficient with python . If you are starting , [start here](https://dev.to/titusnjuguna/part-1-python-101-introduction-to-python-programming-2i4m)

In this article, we are going to dive deep on Abstraction in Python. Below is an overview of this post:
- Definition and importance of Abstraction
- Application of abstraction
- Abstract Classes and Methods 
- Summary

###Definition of abstraction 
Before getting into technical definition of abstraction ,let's first define it with simpler and relatable example. consider your T.V remote, the '+' button ,which is used to increase the volume .You can use the button but you cannot see how operations are carried out. This is a perfect example of abstraction.

Now what is Abstraction in technical term ? In Python, data abstraction can be defined as ,hiding all the irrelevant data/process of an application in order to reduce complexity and increase the efficiency of the program. 

### Application of Abstraction 
Data abstraction has been wholly been implemented in Django framework(Assumption you have basic experience with django).
When creating a model , take an example of Student model:

```python
from django import models

class student(models.Model):
   first_name= models.CharField(max_length=100, blank=True,null=True)
   last_name= models.CharField(max_length=100, blank=True,null=True)
   def __str__(self):
       return self.first_name + "" + self.last_name
```
This is a perfect example of application of abstraction in solving real-world problem. class model inherited from ensure uniformity in definition of a model. As we progress , you will see the consequences of not following the set of instruction provided in the data abstracted.

###Abstract Classes and Methods 
Abstract Class is a class created as a blueprint of an object that can be inherited to create new objects, whereas abstract method is a method that is declared, but does not contain implementation. An abstract method in a base class identifies the functionality that should be implemented by all its subclasses.

*implementation*
To declare an Abstract class, we firstly need to import the abc module. Let us look at an example.

```python
from abc import ABC
class payment_class(ABC):
     #abstract methods goes below
```
Now lets implement a complete abstract class with methods
```python
from abc import ABC
class payment_class(ABC):
     #abstract methods
    @abstractmethod 
    def send_money(self):
         pass
   @abstractmethod 
   def withdraw_money(self):
        pass

```
Apart from abstract method, an abstract class also can have a method and every time an abstract class is inherited it would be called and run.
Let's create a working example:

```python
#Section 1
from abc import ABC,abstractmethod
class payment_class(ABC):
  def welcome_text(self,x):
    print(f'Welcome to {x}')
  #abstract methods
  @abstractmethod 
  def send_money(self):
    pass
  @abstractmethod 
  def withdraw_money(self):
    pass
#Section 2
# Create payment service 
class Terminal_pay(payment_class):
  def __init__(self,total,tax,amount,sendcharges,commission,balance=0):
    self.amount = amount
    self.sendcharges = sendcharges
    self.commission = commission
    self.tax = tax
    self.balance = balance
    self.total = self.tax + self.commission + self.amount + self.sendcharges
  def send_money(self):
    self.balance = self.balance - self.total
    print(f'Send USD. {self.amount} at USD. {self.sendcharges} commission charged USD.{self.commission} give USD. {self.total}.Your balance is USD. {self.balance}')
  def withdraw_money(self):
    self.balance = self.balance - self.total
    print(f'USD.{self.amount} withdrawn at {self.sendcharges} give {self.total}')

safariPay = Terminal_pay(total=0,tax=16,sendcharges=10,commission=10,amount=200)
safariPay.welcome_text("safariPay")
safariPay.send_money()

```
```bash
Output:
Welcome to safariPay
Send USD. 200 at USD.10 commission charged USD.10 give USD.236.Your balance is USD.-236
```
###Explanation
In this example(Divided into two sections), Section 1 is importing ABC,abstractmethod from abc module, declaration of Abstract Base Class with a generic method that has an argument X declared and an Abstract methods : send_money,withdraw_money.

In section 2, We create a payment service that inherits from the payment_class Abstract Base class. The payment_class we create a constructor and what follows is definition of the methods:withdraw_money,send_money and lastly we instantiate the Terminal_class.

Follow me for more update