January 13, 2021

Abstraction in Python With Examples

In this post we’ll see the usage of OOPS concept Abstraction in Python.

What is Abstraction

Abstraction means hiding the complexity of the implementation and just exposing the essential features to the user. As an example you can take any electronics item where you interact with the product using buttons and switches to turn it on and off or increase and decrease the volume or speed. The real complexity, how that functionality is implemented is hidden from us.

In the context of object oriented programming Abstraction means exposing just the end points (methods) and hiding the real implementation from the end user.

Abstraction in Python

Abstraction in Python is achieved by using abstract classes and interfaces.

Abstract class is a class that contains one or more abstract methods. Abstract methods are the methods that don’t contain any implementation, sub classes that inherit from the abstract class should provide implementation for the abstract methods. Abstract class can have regular methods (methods with method body) too so you can say that Abstract class generally provides incomplete functionality providing implementation for the common methods while leaving the specific implementation to the sub-classes.

An interface provides just the method signatures without method bodies. Sub-classes should provide implementation for all the methods defined in an interface. Python doesn't support creation of interface through any separate keyword, you will have to define an interface using abstract class itself. If you create an abstract class which contains only abstract methods that acts as an interface in Python.

Abstraction in Python using abstract class

Let’s see an example of abstraction in Python using an abstract class. For declaring an Abstract class you need to import the abc module.

In the example we have an Abstract class User that has one concrete method display_user() and one abstract method process_fee().

from abc import ABC, abstractmethod

class User(ABC):
  def __init__(self, name, num_of_months):
    self.name = name
    self.num_of_months = num_of_months

  # concrete method
  def display_user(self):
    print('User %s subscribed for %d months' % (self.name, self.num_of_months))

  # abstract method
  @abstractmethod
  def process_fee(self):
    pass

There are two sub classes inheriting from User and implementing the abstract method process_fee().

class PlatinumUser(User):
  PLATINUM_PACKAGE = 2200

  def process_fee(self):
      return self.num_of_months * PlatinumUser.PLATINUM_PACKAGE


class GoldUser(User):
  Gold_PACKAGE = 1500

  def process_fee(self):
      return self.num_of_months * GoldUser.Gold_PACKAGE

As a user we just know that we have to call process_fee() method, we are abstracted from the actual implementation of the method which differs for different child classes of User.

obj = PlatinumUser('Mike Dallas', 8)
obj.display_user()
fee = obj.process_fee()
print('Fee is', fee)

obj = GoldUser('Goldie Hawn', 6)
obj.display_user()
fee = obj.process_fee()
print('Fee is', fee)

obj = PlatinumUser('Ashish Mishra', 10)
obj.display_user()
fee = obj.process_fee()
print('Fee is', fee)
Output
User Mike Dallas subscribed for 8 months
Fee is 17600
User Goldie Hawn subscribed for 6 months
Fee is 9000
User Ashish Mishra subscribed for 10 months
Fee is 22000

That's all for the topic Abstraction in Python With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment