Nearby lessons
110 of 159Python - Encapsulation
- Understand the concept of encapsulation
- Know the advantages of encapsulation
- Create private variables using double-underscore prefixes
- Understand how name mangling renames private variables
- Use getter and setter methods for controlled data access
What is Encapsulation?
Encapsulation is one of the fundamental principles of Object-Oriented Programming.
It is the process of binding data (variables) and the methods that operate on that data inside a single unit.
Encapsulation also restricts direct access to the data. The data is hidden inside the class, and the outside world interacts with it only through well-defined methods.
In Python, encapsulation is achieved by using private variables and getter/setter methods.
Why is Encapsulation Required?
- It hides the internal data of an object from the outside world.
- It prevents accidental or incorrect modification of data.
- It allows validation logic before a value is stored.
- It makes the code easier to maintain and extend.
- It improves security and reliability of the application.
Private Variables
By default, all instance variables in Python are public, meaning they can be accessed from anywhere.
To make an instance variable private, we prefix its name with a double underscore (__).
Explanation
Here, __name and __marks are private variables. They are not accessible directly from outside the class.
What is Name Mangling?
When we write a variable with a double-underscore prefix, Python internally renames that variable.
This automatic renaming is called Name Mangling.
The variable is renamed using the following pattern:
Example
For the class Student, the variable __name becomes:
Accessing a Private Variable Directly
Explanation
Because of name mangling, __name does not exist under that exact name. It exists as _Student__name.
Hence, accessing s.__name directly raises an AttributeError.
Accessing a Private Variable Using Name Mangling
Explanation
Although direct access is blocked, the mangled name _Student__name can still be used to reach the variable.
However, this is not recommended. The proper way to access and modify private data is through getter and setter methods.
Setter Methods
A Setter Method is an instance method used to assign or update the value of an instance variable.
Instead of modifying variables directly, setter methods provide a controlled way to update object data.
Setter methods improve data security and make the program easier to maintain.
Getter Methods
A Getter Method is an instance method used to retrieve the value of an instance variable.
Getter methods provide controlled access to object data without exposing the internal variables directly.
They are commonly used together with setter methods.
Syntax
Setter Method
Getter Method
Characteristics of Getter and Setter Methods
- Setter methods update object data.
- Getter methods return object data.
- Both are instance methods and use the
selfparameter. - They work with instance variables.
- They improve encapsulation and data protection.
- Validation logic can be added inside setter methods before updating values.
- Getter methods provide read-only access when required.
Complete Program
The following program uses getter and setter methods to control access to a Student's data. The setter for marks validates the value before storing it.
Explanation
Setter Methods
setName() updates the student's name.
setMarks() updates the student's marks only if the value is between 0 and 100.
This demonstrates how setter methods can validate data before storing it.
Getter Methods
getName() returns the student's name.
getMarks() returns the student's marks.
Instead of directly accessing the variables, the program retrieves their values through getter methods.
This provides better control over the object's data.
Setter vs Getter Methods
| Feature | Setter Method | Getter Method |
|---|---|---|
| Purpose | Assign or update values | Retrieve values |
| Returns Value | No | Yes |
| Accepts Parameters | Yes | No |
| Works With | Instance Variables | Instance Variables |
| Validation | Possible | Usually Not Required |
| Common Prefix | set | get |
Advantages of Encapsulation
- Protects object data from accidental modification.
- Allows validation before data is stored.
- Keeps related data and methods together.
- Makes the code easier to maintain and modify.
- Increases the security of the application.
- Improves code reusability.
- Encapsulation binds data and the methods that operate on it inside a single unit
- A variable prefixed with __ (double underscore) is private to the class
- Name mangling renames __variable to _ClassName__variable
- Private variables raise an AttributeError when accessed directly from outside
- Getter and setter methods provide controlled and validated access to object data