How to use Encapsulation and Data Abstraction In Python

Spread the love

Define encapsulation in Python?

Encapsulation In Python
Encapsulation In Python

Encapsulation is the part of object-oriented programming (OOP). Encapsulation is packing of data and the methods or function under a single unit. This puts restriction by some members which are protected member and private member so that outside the class we cannot use the methods directly, we have to use some special syntax to access these class to prevent the data modification. In python we denote private member by prefix i.e. Single underscore () or double underscore (_).
A class is an example of encapsulation as it encloses in all the data that is member functions, variables etc.
Encapsulation hides the information/data from others. In this the user will not have the inner implementation of class and the user is also not aware of how the class storing the values in the variables. It has great flexibility as we can make variables of the class according to our need. We can make read-only method as well as write-only method.
Encapsulation is all about hiding the way or method of working and just providing the working model.

Protected member
Protected member is accessible only from within the class and its subclasses. To find out which member is protected its prefix is single underscore (_).

Private member
These member are similar to protected members, the difference is that the class members declared private should neither be accessed outside the class nor by any base class.

How do you do data abstraction in Python?

Data Abstraction In Python
Data Abstraction In Python

bstraction in python is done by using abstract classes and interfaces.
A class which contains one or more abstract method is called abstract class.
Abstraction is hiding the implementation and complexity and showing declaration and essential feature of the object.
Real world example of abstraction is car, bike as we see from outside is a gear, steering, brake which is declaration but we cannot see interior implementation as how the motor is running, how tire is moving and all internal work.
We use abstract base classes so that we can define a common application interface (API) for a set of subclasses.
By default, Python does not provide abstract classes. Python comes with a module which provides the base for defining Abstract Base classes (ABC) and that module name is ABC
NOTE: In Python there is no support for creating interfaces explicitly, you will have to use abstract class.
Abstraction is implemented using interface and abstract class
An abstract class is a class that generally provides incomplete functionality and contains one or more abstract methods.
we can declare an abstract method by using @abstractmethod decorator.

Write a Python program to sum of two given integers. However, if the sum is between 15 to 20 it will return 20.

def sum(a, b):
    sum = a + b
    if sum in range(15, 20):
        return 20
        return sum

a = int(input())
b = int(input())
print(sum(a, b))

Summary :

In this article we saw what is Encapsulation In Python and How Do You Do Data Abstraction In Python? so about this article you have any query then free to ask me

Name of Intern who share this Task : Yanni Goel

Github link :

sachin Pagar

I am Mr. Sachin pagar Embedded software engineer, the founder of Sach Educational Support(Pythonslearning), a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Leave a Reply