DEV Community

Cover image for Encapsulation in Python
ckorley4
ckorley4

Posted on

Encapsulation in Python

The process of wrapping up variables and methods into a single entity is known as Encapsulation. It is one of the underlying concepts in object-oriented programming (OOP). It acts as a protective shield that puts restrictions on accessing variables and methods directly, and can prevent accidental or unauthorized modification of data. Encapsulation also makes objects into more self-sufficient, independently functioning pieces.

Why do we need Encapsulation in Python?
The advantages of Encapsulation in Python can be summed up as follows –

  1. Encapsulation provides well-defined, readable code
    The primary advantage of using Encapsulation in Python is that as a user, we do not need to know the architecture of the methods and the data and can just focus on making use of these functional, encapsulated units for our applications. This results in a more organized and clean code. The user experience also improves greatly and makes it easier to understand applications as a whole.

  2. Prevents Accidental Modification or Deletion
    Another advantage of encapsulation is that it prevents the accidental modification of the data and methods. Let’s consider the example of NumPy again, if I had access to edit the library, then I might make a mistake in the implementation of the mean function and then because of that mistake, thousands of projects using NumPy would become inaccurate.

  3. Encapsulation provides security
    Encapsulation in Python is achieved through the access modifiers. These access modifiers ensure that access conditions are not breached and thus provide a great user experience in terms of security.

  4. Encapsulation provides reusability
    It is easier to reuse code when it is encapsulated. Creating objects that contain common functionality allows us to reuse them in many settings.

Access Modifiers in Python encapsulation
Sometimes there might be a need to restrict or limit access to certain variables or functions while programming. That is where access modifiers come into the picture.

Now when we are talking about access, 3 kinds of access specifiers can be used while performing Encapsulation in Python. They are as follows :

Public Members
Private Members
Protected Members

Image description

Lets take a look at this example

# Encapsulation in Python
# A simple example

class Original: 
    def __init__(self): 
        self._protected_variable= 10

# Creating a derived class 
class Derived(Original): 
    def __init__(self): 

        # Calling constructor of  Original Class
        Original.__init__(self) 
        print("Calling the member of the original class: ",  
              self._protected_variable) 

        # Modifying the protected variable: 
        self._protected_variable = 200
        return(f'Protected member outside class:{self._protected_variable}') 

object_one = Derived() 
object_two = Original() 

print("Protected member of Derived: ", object_one._protected_variable) 

print("Protected member of Original: ", object_two._protected_variable) 
Enter fullscreen mode Exit fullscreen mode

Conclusion
Encapsulation in Python refers to the practice of hiding the implementation details of an object or class, and only exposing a public interface for interacting with it.

Encapsulation is achieved through the use of access modifiers such as 'public', 'private', and 'protected'.

Variables and methods declared with the 'private' or 'protected' access modifiers are not accessible from outside the class, while those declared as 'public' are.

Encapsulation helps to promote code reusability, maintainability, and security by preventing external code from accessing or modifying the internal state of an object or class.

The '' or '_' prefix can be used to indicate a variable or method as private, however, it is not enforced by the interpreter and can still be accessed.

In Python, encapsulation is not as strict as in other languages such as Java, as it does not have the concept of protected variables and methods.

https://www.educative.io/answers/what-is-encapsulation-in-python
https://www.scaler.com/topics/python/encapsulation-in-python/

Top comments (0)