Code Crafter IT Innovation
Loading...
  • Bansal Computer Center
  • punjabedu.bansalcenter.com

Python Encapsulation

What is Encapsulation?

Encapsulation is the concept of binding data (variables) and methods (functions) into a single unit called a class and protecting the data from direct access.

In simple words: Data ko class ke andar band karke usko secure rakhna.

Why Encapsulation is Used

Real-Life Example

An ATM Machine is an example of encapsulation: You cannot access balance directly; you use a PIN + buttons, while the internal working remains hidden.

Access Modifiers

Access modifiers decide where a variable or method can be accessed within the program.

TypeSymbolDefinition
PublicNo symbolAccessible from anywhere
Protected_ (Single)Accessible inside class & child class
Private__ (Double)Accessible only inside the class

Public, Protected & Private

1. Public Members

class Student:
    def __init__(self):
        self.name = "Mayank" # Public
        self.marks = 90      # Public

s = Student()
print(s.name)  # Allowed
print(s.marks) # Allowed

2. Protected Members

Single underscore (_) means protected. It is meant for internal or child class use. Python allows access, but it is a convention not to.

class Student:
    def __init__(self):
        self._marks = 85 # Protected

class Result(Student):
    def show(self):
        print(self._marks) # Accessible in child class

3. Private Members

Double underscore (__) makes it private. Strong data protection.

class Student:
    def __init__(self):
        self.__marks = 95 # Private

s = Student()
print(s.__marks) # ERROR: Direct access not allowed

Note: Name Mangling

Python internally changes the name of private variables. print(s._Student__marks) works but is not recommended as it breaks encapsulation.

Getter and Setter Methods

Methods used to read and modify private data safely with conditions.

Getter Method

Used to read private data safely.

def get_marks(self):
    return self.__marks

Setter Method

Used to modify private data with logic (e.g., validation).

def set_marks(self, m):
    if m >= 0 and m <= 100:
        self.__marks = m

Complete Example

class Student:
    def __init__(self):
        self.__marks = 0

    def set_marks(self, m):
        if 0 <= m <= 100:
            self.__marks = m

    def get_marks(self):
        return self.__marks

s = Student()
s.set_marks(92)
print(s.get_marks()) # Output: 92

The @property Decorator

@property allows a method to be accessed like a variable for cleaner syntax.

class Student:
    def __init__(self):
        self.__marks = 50

    @property
    def marks(self): # Acts as getter
        return self.__marks

    @marks.setter
    def marks(self, value): # Acts as setter
        if 0 <= value <= 100:
            self.__marks = value

s = Student()
s.marks = 80   # Easy assignment
print(s.marks) # Easy access

Advantages of Encapsulation

What students Say?

Here are the stdudents...

#

Contact Us

Empower your tech dreams with us. Get in touch!

  • #
  • #
  • #
  • #
  • #