Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4047

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T08:21:09+00:00 2024-11-26T08:21:09+00:00

Simplified O.O.P : Abstraction in Python

  • 61k

Introduction

In previous post , Simplified: Object-oriented Programming Python, 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

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:

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 
Enter fullscreen mode Exit fullscreen mode

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.

from abc import ABC class payment_class(ABC):      #abstract methods goes below 
Enter fullscreen mode Exit fullscreen mode

Now lets implement a complete abstract class with methods

from abc import ABC class payment_class(ABC):      #abstract methods     @abstractmethod      def send_money(self):          pass    @abstractmethod     def withdraw_money(self):         pass  
Enter fullscreen mode Exit fullscreen mode

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:

#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()  
Enter fullscreen mode Exit fullscreen mode

Output: Welcome to safariPay Send USD. 200 at USD.10 commission charged USD.10 give USD.236.Your balance is USD.-236 
Enter fullscreen mode Exit fullscreen mode

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

beginnersooppythonwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.