Understanding Abstraction in Python

Python Abstraction

Inroduction

Today in this tutorial, we are going to discuss the concept of Abstraction in Python for the Object-Oriented Programming approach.

If you are new to OOP, we highly recommend going through our Object-Oriented Programming in Python article.

Basically, Abstraction focuses on hiding the internal implementations of a process or method from the user. In this way, the user knows what he is doing but not how the work is being done.

Let us dig a bit deeper into the topic to find its importance in real life and programming.


What is Abstraction in Python?

In Object Oriented Programming, Inheritance, Polymorphism and Encapsulation go hand in hand. But Abstraction is also an essential element of OOP.

For example, people do not think of a car as a set of thousands of individual parts. Instead they see it as a well-defined object with its own unique behavior. This abstraction allows people to use a car to drive without knowing the complexity of the parts that form the car. They can ignore the details of how the engine transmission, and braking systems work. Instead, they are free to utilize the object as a whole.

A powerful way to manage abstraction is through the use of hierarchical classification. This allows us to layer the semantics of complex systems, breaking them into more manageable pieces. From the outside, a car is a single object. Once inside, you see that the car consists of several subsystems: steering, brakes, sound system, seat belts, etc. In turn, each of these subsystems is made up of smaller units.

The point is that we manage the complexity of the car (or any other complex system) through the use of hierarchical abstractions.

This can also be applied to computer programs using OOP concepts. This is the essence of object-oriented programming.


Abstract Classes and Methods in Python

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

from abc import ABC
class abs_class(ABC):
     #abstract methods

Here, abs_class is the abstract class inside which abstract methods or any other sort of methods can be defined.

As a property, abstract classes can have any number of abstract methods coexisting with any number of other methods. For example we can see below.

from abc import ABC, abstractmethod
class abs_class(ABC):
    #normal method
    def method(self):
        #method definition
    @abstractmethod
    def Abs_method(self):
        #Abs_method definition

Here, method() is normal method whereas Abs_method() is an abstract method implementing @abstractmethod from the abc module.


Python Abstraction Example

Now that we know about abstract classes and methods, let’s take a look at an example which explains Abstraction in Python.

from abc import ABC, abstractmethod
class Absclass(ABC):
    def print(self,x):
        print("Passed value: ", x)
    @abstractmethod
    def task(self):
        print("We are inside Absclass task")

class test_class(Absclass):
    def task(self):
        print("We are inside test_class task")

class example_class(Absclass):
    def task(self):
        print("We are inside example_class task")

#object of test_class created
test_obj = test_class()
test_obj.task()
test_obj.print(100)

#object of example_class created
example_obj = example_class()
example_obj.task()
example_obj.print(200)

print("test_obj is instance of Absclass? ", isinstance(test_obj, Absclass))
print("example_obj is instance of Absclass? ", isinstance(example_obj, Absclass))

Output:

Python Abstraction Example
Python Abstraction Example

Here,

Absclass is the abstract class that inherits from the ABC class from the abc module. It contains an abstract method task() and a print() method which are visible by the user. Two other classes inheriting from this abstract class are test_class and example_class. Both of them have their own task() method (extension of the abstract method).

After the user creates objects from both the test_class and example_class classes and invoke the task() method for both of them, the hidden definitions for task() methods inside both the classes come into play. These definitions are hidden from the user. The abstract method task() from the abstract class Absclass is actually never invoked.

But when the print() method is called for both the test_obj and example_obj, the Absclass’s print() method is invoked since it is not an abstract method.

Note: We cannot create instances of an abstract class. It raises an Error.


Conclusion

So today in this tutorial, we understood the concept of Abstraction in Python.

For any further related questions, feel free to use the comments below.


References