When working with an Object Oriented Programming language like Python, encapsulation in Python is one of the 4 important concepts to understand. The other three are, , and abstraction.
What is Encapsulation?
When working with classes and dealing with sensitive data, providing global access to all the variables used within the program is not a good choice. Encapsulation offers a way for us to access the required variables without providing the program full-fledged access to any of those variables.
Updating, modifying, or deleting data from variables can be done through the use of methods that are defined specifically for the purpose. The benefit of using this approach to programming is improved control over the input data and better security.
What is Encapsulation in Python?
The concept of encapsulation is the same in all object-oriented programming languages. The difference is seen when the concepts are applied to particular languages.
Compared to languages like Java that offer access modifiers (public or private) for variables and methods, Python provides access to all the variables and methods globally.
Check the below demonstration of how variables can easily be accessed.
Since we do not have access modifiers in Python, we will use a few different methods to control the access of variables within a Python program.
Methods to Control Access
There are multiple methods that are offered by Python to limit variable and method access across the program. Let’s go over the methods in detail.
Using Single Underscore
A common Python programming convention to identify a private variable is by prefixing it with an underscore. Now, this doesn’t really make any difference on the compiler side of things. The variable is still accessible as usual. But being a convention that programmers have picked up on, it tells other programmers that the variables or methods have to be used only within the scope of the class.
See the below example:
It’s clear that the variable access is unchanged. But can we do anything to really make it private? Let’s have a look further.
Using Double Underscores
If you want to make class members i.e. methods and variables private, then you should prefix them with double underscores. But Python offers some sort of support to the private modifier. This mechanism is called Name mangling. With this, it is still possible to access the class members from outside it.
In Python, any identifier with __Var is rewritten by a python interpreter as _Classname__Var, and the class name remains as the present class name. This mechanism of changing names is called Name Mangling in Python.
In the below example, in Class person, the age variable is changed and it’s prefixed by leading double underscores.
You can observe that variables are still be accessed using methods, which is a part of the class. But you cannot access age directly from outside, as it is a private variable.
Using Getter and Setter methods to access private variables
If you want to access and change the private variables, accessor (getter) methods and mutators(setter methods) should be used, as they are part of Class.
Benefits of Encapsulation in Python
Encapsulation ensures better data flow and also protects the data from outside sources. The concept of encapsulation makes the code self-sufficient. It is very helpful in the implementation level, as it prioritizes the ‘how’ type questions, leaving behind the complexities. You should hide the data in the unit to make encapsulation easy and also to secure the data.
What is the need for Encapsulation in Python
The following reasons show why developers find the Encapsulation handy and why the Object-Oriented concept is outclassing many programming languages.
- Encapsulation helps in achieving the well-defined interaction in every application.
- The Object-Oriented concept focuses on the reusability of code in Python. (DRY – Don’t Repeat Yourself).
- The applications can be securely maintained.
- It ensures the flexibility of the code through a proper code organization.
- It promotes a smooth experience for the users without exposing any back-end complexities.
- It improves the readability of the code. Any changes in one part of the code will not disturb another.
- Encapsulation ensures data protection and avoids the access of data accidentally.
Encapsulation in Python is, the data is hidden outside the object definition. It enables developers to develop user-friendly experience. This is also helpful in securing data from breaches, as the code is highly secured and cannot be accessed by outside sources.
Main Concepts of Object-Oriented Programming (OOPs)
- Data Abstraction
A class is a collection of objects. A class contains the blueprints or the prototype from which the objects are being created. It is a logical entity that contains some attributes and methods.
To understand the need for creating a class let’s consider an example, let’s say you wanted to track the number of dogs that may have different attributes like breed, age. If a list is used, the first element could be the dog’s breed while the second element could represent its age. Let’s suppose there are 100 different dogs, then how would you know which element is supposed to be which? What if you wanted to add other properties to these dogs? This lacks organization and it’s the exact need for classes.
Some points on Python class:
- Classes are created by keyword class.
- Attributes are the variables that belong to a class.
- Attributes are always public and can be accessed using the dot (.) operator. Eg.: Myclass.Myattribute
Class Definition Syntax:
class ClassName: # Statement-1 . . . # Statement-N
Example: Creating an empty Class in Python
In the above example, we have created a class named dog using the class keyword.
The object is an entity that has a state and behavior associated with it. It may be any real-world object like a mouse, keyboard, chair, table, pen, etc. Integers, strings, floating-point numbers, even arrays, and dictionaries, are all objects. More specifically, any single integer or any single string is an object. The number 12 is an object, the string “Hello, world” is an object, a list is an object that can hold other objects, and so on. You’ve been using objects all along and may not even realize it.
An object consists of :
- State: It is represented by the attributes of an object. It also reflects the properties of an object.
- Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects.
- Identity: It gives a unique name to an object and enables one object to interact with other objects.
Example: Creating an object
This will create an object named obj of the class Dog defined above. Before diving deep into objects and class let us understand some basic keywords that will be used while working with objects and classes.
- Class methods must have an extra first parameter in the method definition. We do not give a value for this parameter when we call the method, Python provides it.
- If we have a method that takes no arguments, then we still have to have one argument.
- This is similar to this pointer in C++ and this reference in Java.
When we call a method of this object as myobject.method(arg1, arg2), this is automatically converted by Python into MyClass.method(myobject, arg1, arg2) – this is all the special self is about.
The __init__ method
The __init__ method is similar to constructors in C++ and Java. It is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object.
Now let us define a class and create some objects using the self and __init__ method.
Example 1: Creating a class and object with class and instance attributes
Rodger is a mammal Tommy is also a mammal My name is Rodger My name is Tommy
Example 2: Creating Class and objects with methods
My name is Rodger My name is Tommy
Inheritance is the capability of one class to inherit the properties from another class. The class that derives properties is called the derived class or child class and the class from which the properties are being derived is called the base class or parent class. The benefits of inheritance are:
- It represents real-world relationships well.
- It provides the reusability of a code. We don’t have to write the same code again and again. It also allows us to add more features to a class without modifying it.
- It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.
Types of Inheritance
Single-level inheritance enables a derived class to inherit characteristics from a single-parent class.
Multi-level inheritance enables a derived class to inherit properties from an immediate parent class which in turn inherits properties from his parent class.
Hierarchical level inheritance enables more than one derived class to inherit properties from a parent class.
Multiple level inheritance enables one derived class to inherit properties from more than one base class.
Example: Inheritance in Python
Rahul 886012 My name is Rahul IdNumber: 886012 Post: Intern
In the above, we have created two classes i.e. Person (parent class) and Employee (Child Class). The Employee class inherits from the Person class. We can use the methods of the person class through employee class as seen in the display function in the above code. A child class can also modify the behavior of the parent class as seen through the details() method.
Polymorphism simply means having many forms. For example, we need to determine if the given species of birds fly or not, using polymorphism we can do this using a single function.
Example: Polymorphism in Python
There are many types of birds. Most of the birds can fly but some cannot. There are many types of birds. Sparrows can fly. There are many types of birds. Ostriches cannot fly.
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data. To prevent accidental change, an object’s variable can only be changed by an object’s method. Those types of variables are known as private variables.
A class is an example of encapsulation as it encapsulates all the data that is member functions, variables, etc.
Example: Encapsulation in Python
In the above example, we have created the c variable as the private attribute. We cannot even access this attribute directly and can’t even change its value.
It hides the unnecessary code details from the user. Also, when we do not want to give out sensitive parts of our code implementation , data abstraction came.
Data Abstraction in Python can be achieved through creating abstract classes.
- Object-Oriented Programming makes the program easy to understand as well as efficient.
- Since the class is sharable, the code can be reused.
- Data is safe and secure with data abstraction.
- Polymorphism allows the same interface for different objects, so programmers can write efficient code.