Abstraction in Python Programming – (OOPs)

Abstraction in Python: Data abstraction in python and data encapsulation in python programming are related to each other. The main point that is necessary here to note is that data abstraction is only possible to achieve through encapsulation.

Encapsulation means storing or placing data in a single place to make it easily readable and compact in one place. Whereas data abstraction in python programming means to hide internal functionalities that are performing on the application using codes and to show only essential information (class attributes).

 

By terms abstracting, we mean something to provide a name to things so that by seeing the name, the responsible programmer may understand the idea of what the whole program is doing behind the scene.

abstraction and encapsulation in python

Abstraction and Encapsulation in Python

  • When we are developing any large or enterprise application. Then it’s a good practice to use the concepts of data encapsulation and data abstraction in the coding approach.
  • Both terms are different in meaning but indirectly related to each other.
  • There are two types of programming approaches, Procedural programming and Object-oriented programming.
  • Encapsulation and Abstraction come under an object-oriented approach which is designed for writing easy and readable codes.
  • Encapsulation means storing the code of each functionality in one place. While abstraction is responsible for presenting only non-sensitive information to the user by hiding the sensitive information.

Advantages of Abstraction in Python:

  • Abstraction helps in reducing programming efforts and reduce coding complexity.
  • It is used to hide unwanted details from the user.
  • It allows focusing on the main concept.

Let’s try to understand the concept of abstraction in python programming by a real-world example.
Suppose you working in the banking sector and a person came to you regarding opening an account. A person is giving you the below details to open an account:

data abstraction in python programming

But actually, not all the given information is required to open an account in the bank. We need to use only the useful information that is necessary to open an account in a bank. That is name, address, tax information, contact number etc.

abstraction in python programming

The details bordered in red is enough to open an account in a bank. Hence, for this purpose, the abstraction concept is used.

Conclusion:
Data abstraction doesn’t mean avoiding storing data, that is not necessary for some specific operation. Being a programmer, it is a good practice to define a separate method in which you can store less important data so that it can be used later when needed.

Example elaborating the approach of data encapsulation and abstraction in Python.

class Person:
    def __init__(self):
        self.name = "Jack Matte"

    def bio(self):
        self.addr = "Bakers street, London"
        self.taxInfo = "HUAPK29971"
        self.contact = "01-777-523-342"
        print(self.addr, self.taxInfo, self.contact)

    def interest(self):
        self.favFood = "Chinese"
        self.hobbies = "Python Programming"
        self.bloodGroup = "A+"
        print(self.favFood, self.hobbies, self.bloodGroup)

obj = Person()
print(obj.name)
obj.bio()
obj.interest()

Detailed Explanation:

  • There is no rocket science or special logic in this program.
  • This is just to make you visualize what actually data encapsulation and abstraction means.
  • Here in this program, the encapsulation we achieved by storing the code of each module at a specific place i.e different methods.
  • The abstraction we achieved by dividing the complete information or data into category i.e. [ bio() and interest() ] so that it can be reused.

ImportantMany students always remain confused between the term abstraction and abstract class. Here abstract class is not the same as abstraction in python. An abstract class is another concept. Learn more about Abstract class in Python.