Overview of Encapsulation in Python
A kind of security mechanism provided by Python programming language, much like other object-oriented programming languages, which brings necessary and adequate restrictions on variables and methods so that they are not subjected to intended or unintended incorrect use, such variables and methods are those units of the program that denote the sensitive and important part of the program, such type of mechanism is termed as encapsulation in Python programming language.
Name Mangling in Python
Python implies a restriction on the access of data. There are no explicit access modifiers but access can be controlled via Name Mangling in Python. By default, all methods and variables are public in Python. So if any identifier has two leading underscores it becomes a non-public instance in Python. We would be specifying non-public instance variables and methods here to understand encapsulation better. The scope of non-public instance method is only within its own class, and it starts with one underscore or two underscores i.e. single “_” or “__” double before a variable or a method. The scope of a non-public instance variable is also in its own class or by the method in which it is defined and it also starts with two underscores. If we happen to miss out on the two underscores then that method is considered as a public method. Before we understand Encapsulation in Python we need to understand how public, and non-public instance variables and methods work.
Non-Public Instance Variables
In Python, there is no term as “private” as no attribute is really private in Python(We will be discussing the underlying alternative later in the article). Instead, we refer them as Non-Public Instance Variable. That is how we would refer the private variables and private methods hereby.
Here is a small snippet of the code in python for non-public instance variables:
We use the self keyword because we are accessing the class variable. The output is:
To fetch a confirmation on the scope of the non-public instance variable we add the print(abc.__a) to the code:
And the output returned is:
Thus, we see that we can not use non-public instance variables outside the class.
Non-Public Instance Methods
A non-public instance function cannot be called on the object directly but only within the class.
In this code snippet, we have two methods, one non-public instance, and another public method. The public method is easily called by creating an object of the class abc and then its output gets printed.
When a similar approach is tried for the non-public instance method we face the following error:
This program will produce the following output:
Therefore it is not possible to access a non-public instance method by object creation. To access the non-public instance method we can call the __show1() method from the show2() method of the same class. Here is how we can display the non-public instance method from the public method of the same class.
Thus the non-public instance method cannot be called outside its class.
The indirect way for accessing the Non-Public Instance Variable
For legit reasons non-public instance variables cannot be accessed beyond the class, but there is an indirect way to modify the value of the non-public instance variable and access using objects. We shall have a look at the below code snippet:
In the above program we have public methods getNumber(self,num) and displayNumber() and one non-public instance variable. On calling the first method we assign the value to the non-public instance variable, and that is called in the displayNumber() method.
The setter method is used to set the value of the non-public instance variable. This is helpful in the scenarios when you may want to change the value of a non-public instance variable after the creation of objects for a specific variable.
Encapsulation helps in establishing a better flow of data, and also the protection of the data. The concept of encapsulation makes the code self-sufficient. Encapsulation provides a great help at the implementation level as it primarily focuses on the question “how” leaving the complex “when/where” and its complexities behind. Hiding the data into a unit makes the encapsulation easier and also secures the data.
Why do we need Encapsulation in Python?
Below are several listed out reasons why developers would find the concept of encapsulation handy and why the Object-oriented concepts are ruling in most of the programming languages that are running in the present day scenario.
- There is a need for well-defined interaction in every application, encapsulations help in achieving that.
- The concept of Object Orientation Programming in python focuses on making reusable code. This is also abbreviated as DRY(Don’t Repeat Yourself).
- Maintenance of application is easier and there is security ensured.
- Clarity on the coding procedure as developers are to be concerned at the objective of the class and the complexities are dealt with systematically.
- Proper organization of the codes helps with the flexibility of the code and also aids in Unit Testing.
- Users find it easy to use the system as they are hidden away from the complex design involved at the backend.
- Having all similar data in one place and encapsulated increases the cohesion inside a module.
- Improves the readability of the codes and changes in one part of the code do not disrupt other parts of the code.
- Encapsulation prevents the part of the code from being accessed by accident, but not intentionally as objects hold critical data for applications and it should be changed anywhere in the code.
In simpler terms Encapsulation in Python means the internal representation of an object is generally hidden from the view outside the object’s definition. This helps the developer develop a user-friendly experience for the end-user and there is protection from a security breach as the codes are made secure.
This is a guide to Encapsulation in Python. Here we discuss the needs of encapsulation in python along with its Non-public Instance Variables and Instance Methods. You may also look at the following articles to learn more –