.
Exploring Abstraction in Python OOP With Code Examples

Exploring Abstraction in Python OOP With Code Examples

Abstraction is a crucial concept in software engineering, and it refers to the ability to hide implementation details while exposing the essential features of a system. This concept is especially useful in object-oriented programming, where it is implemented using abstract base classes.

In this blog post, we will discuss the basics of abstraction and abstract base classes in Python.

Abstraction Base

Abstraction is a way of representing complex systems by focusing on the essential features that are necessary for a particular purpose. Abstraction is implemented in programming languages through various techniques, such as encapsulation, inheritance, and polymorphism.

Abstraction base is a fundamental concept in object-oriented programming, and it is implemented through abstract base classes (ABCs). An abstract base class is a class that cannot be instantiated, and it provides a set of methods and properties that must be implemented by its derived classes.

Abstract Base Classes

An abstract base class is a class that provides a set of methods and properties that must be implemented by its derived classes. Abstract base classes are defined using the ‘ABC’ module in Python.

Let’s start by looking at an example. Consider a class ‘’Animal that represents animals. We can define an abstract base class for ‘’Animal as follows.

import abc

class Animal(metaclass=abc.ABCMeta):

@abc.abstractmethod
def speak(self):
pass

In the above code, we define an abstract base class Animal using the abc.ABCMeta metaclass. The class contains one abstract method speak(), which must be implemented by its derived classes.

Abc Module

Abc module provides a means to define Abstract Base Classes(ABCs) in python

Two ways to define Abstract Base class

1. metaclass ABCMeta

2. helper class ABC

Register to an Abstract Base class

class Printing(ABC):
@abstractmethod
def print(self, content):
pass

class GlassPrint():
def print(self, content):
print(“Print on glass”)

Printing.register(GlassPrint)

We can inherit from an Abstract Base class like

class Website(ABC):
def create_session(self, user):
print(“The session created for user: “, user)

def content_access(self):
print(“User is granted content access”)

def destroy_session(self, user):
print(“The session destroyed for user: “, user)

@abstractmethod
def authenticate(self, user_cred):
pass

@abstractmethod
def registration(self, user_detail):
pass

class SocialNetwork(Website):
def authenticate(self, user_cred):
print(“Authentication process for: “, user_cred)

def registration(self, user_detail):
print(“Register process for: “, user_detail)

Abstract Base Class as Website

Here we have created an Abstract Base class as Website which contains concrete and abstract methods and the SocialNetwork class inherits from this website class and implements the abstract methods.

Now if we create object

fb = SocialNetwork()
fb.registration(“User details”)
fb.authenticate(“User creds”)
fb.create_session(“User A”)
fb.content_access()
fb.destroy_session(“User A”)

The Output is

Register process for:  User details
Authentication process for:  User creds
The session created for user:  User A
User is granted content access
The session destroyed for user:  User A

Another Implementation of Website Abstract Class

class UserForum(Website):
def authenticate(self, user_cred):
print(“Authentication process for: “, user_cred)

def registration(self, user_detail):
print(“Register process for: “, user_detail)

def post_content(self, user_cred, content):
self.authenticate(user_cred)
print(f”{content} posted by: {user_cred}”)

def view_content(self, user_cred=”Guest User”):
self.authenticate(user_cred)
print(“Content Viewed by “, user_cred)

Here we try to achieve the functionality of a user forum portal consisting of abstract methods implementation and other instance methods to achieve the functionality of content display and posting content.

When we create an object of UserForum and try to achieve as

forum = UserForum()
forum.registration(“User A’s details”)
forum.post_content(“User A”, “New Content”)

The output is

Register process for:  User A’s details
Authentication process for:  User A
New Content posted by: User A

We can also achieve Guest user functionality

forum = UserForum()
forum.view_content()

The output is

Authentication process for:  Guest User
Content Viewed by  Guest User

And also like this

forum = UserForum()
forum.registration(“User B’s detail”)
forum.create_session(“User B”)
forum.view_content(“User B”)
forum.post_content(“User B”, “New Content”)
forum.destroy_session(“User B”)

The output is

Register process for:  User B’s detail
The session created for user:  User B
Authentication process for:  User B
Content Viewed by  User B
Authentication process for:  User B
New Content posted by: User B
The session destroyed for user:  User B

Conclusion

In this blog post, we discussed the basics of abstraction and abstract base classes in Python. We learned that abstraction is a way of representing complex systems by focusing on the essential features that are necessary for a particular purpose. We also learned that abstract base classes are classes that provide a set of methods and properties that must be implemented by their derived classes.

In Python, we can define abstract base classes using the ‘ABC’ module. We can then define derived classes that inherit from these abstract base classes and implement their abstract methods. This allows us to create complex systems that are modular, maintainable, and extensible.

Share this article
Shareable URL
Prev Post

Mapping Services for Efficient Resource Management and Infrastructure Maintenance

Next Post

Optimizing Data Analysis by Understanding Relationship Models in Power BI

Read next