What students Say?
Here are the stdudents...
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.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 decide where a variable or method can be accessed within the program.
| Type | Symbol | Definition |
|---|---|---|
| Public | No symbol | Accessible from anywhere |
| Protected | _ (Single) | Accessible inside class & child class |
| Private | __ (Double) | Accessible only inside the class |
class Student:
def __init__(self):
self.name = "Mayank" # Public
self.marks = 90 # Public
s = Student()
print(s.name) # Allowed
print(s.marks) # Allowed
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
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
Python internally changes the name of private variables. print(s._Student__marks) works but is not recommended as it breaks encapsulation.
Methods used to read and modify private data safely with conditions.
Used to read private data safely.
def get_marks(self):
return self.__marks
Used to modify private data with logic (e.g., validation).
def set_marks(self, m):
if m >= 0 and m <= 100:
self.__marks = m
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
@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
Here are the stdudents...
Empower your tech dreams with us. Get in touch!




