B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python Encapsulation | LiveCodeProgramming

Encapsulation in Python

Encapsulation is the concept of wrapping data (variables) and code (methods) together as a single unit. In Python, we achieve encapsulation by using private or protected access modifiers.

Encapsulation का मतलब है डेटा और functions को एक single unit में बांधना। Python में इसे private और protected members से achieve किया जाता है।

1. Public Attributes
class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

e1 = Employee('Aryan', 50000)
print(e1.name, e1.salary)

Output:

Aryan 50000

Attributes are accessible from outside the class.

Attributes को class के बाहर से access किया जा सकता है।

2. Protected Attributes
class Employee:
    def __init__(self, name, salary):
        self._name = name
        self._salary = salary

e1 = Employee('Naincy', 60000)
print(e1._name, e1._salary)

Output:

Naincy 60000

Protected attributes use '_' prefix. They can still be accessed but not recommended.

Protected attributes को '_' से prefix किया जाता है। इन्हें access किया जा सकता है लेकिन ये recommended नहीं है।

3. Private Attributes
class Employee:
    def __init__(self, name, salary):
        self.__name = name
        self.__salary = salary

e1 = Employee('Jay', 70000)
# print(e1.__salary)  # Error: AttributeError

Output:

AttributeError: 'Employee' object has no attribute '__salary'

Private members use '__' and can't be accessed directly from outside.

Private members '__' से prefix किए जाते हैं और class के बाहर से access नहीं किए जा सकते।

4. Access Private via Method
class Employee:
    def __init__(self, name, salary):
        self.__name = name
        self.__salary = salary
    def show(self):
        print(f'{self.__name} earns {self.__salary}')

e1 = Employee('Shrutika', 80000)
e1.show()

Output:

Shrutika earns 80000

Access private variables using a method inside the class.

Private variables को class के अंदर method से access किया गया।

5. Getter Method
class Employee:
    def __init__(self, name, salary):
        self.__salary = salary
    def get_salary(self):
        return self.__salary

e1 = Employee('Ravi', 90000)
print(e1.get_salary())

Output:

90000

Getter method is used to get private value.

Getter method से private value को access किया गया।

6. Setter Method
class Employee:
    def __init__(self, name, salary):
        self.__salary = salary
    def set_salary(self, value):
        self.__salary = value
    def get_salary(self):
        return self.__salary

e1 = Employee('Dev', 95000)
e1.set_salary(100000)
print(e1.get_salary())

Output:

100000

Setter method updates private data safely.

Setter method से private डेटा को safely update किया गया।

7. Name Mangling
class Employee:
    def __init__(self):
        self.__secret = 'hidden'
e1 = Employee()
print(e1._Employee__secret)

Output:

hidden

Private variables can be accessed using _ClassName__var (name mangling).

Private variables को name mangling से access किया जा सकता है।