Home >> Python Tutorial >> Encapsulation
Edit Template
Edit Template

Encapsulation in Hindi

What is Encapsulation

जब हम data member (variables, attributes, etc.) और methods को एक single unit (class) में अंदर बंद करते है तो इसे Encapsulation कहा जाता है।

Encapsulation OOP का एक important और most useful concept है, python में किसी data को कोई भी direct access कर सकता है उस data को देख सकता है और उसमे बदलाव भी कर सकता है इसीलिए अगर हमे data को direct access से protect करना हो तो हम Encapsulation का use करते है।

Access Modifiers

Python में कोई strictly private variables or methods तो नहीं होते but उनके name में कुछ modification करके हम उन variables or methods को direct access के protect कर सकते है, Python में 3 types के Access Modifiers होते है-

Public Members

Python में जब भी हम कोई data members (variables, methods, etc.) create करते है तो वो public members ही होते है उनके कोई भी directly access कर सकते है।

🐍
Example 1.py
Copy to clipboard
class Student:
    def __init__(self, name):
        self.name = name   # public attribute

s = Student("Vikas")
print(s.name)   # Direct access

Protected Members

जब हम किसी data members (variables, methods, etc.) के name के आगे single underscore “_” लगते है तो वो protected member बन जाता है, protected members को सिर्फ class और subclass में ही access किया जा सकता है, outside में इसे access नहीं किया जा सकता है।

🐍
Example 2.py
Copy to clipboard
class Student:
    def __init__(self, name):
        self._name = name   # protected

s = Student("Vikas")
print(s._name)   # Accessible, but not recommended

Private Members

जब हम किसी data members (variables, methods, etc.) के name के आगे double underscore “__” लगते है तो वो private member बन जाता है, private member को class के बाहर access नहीं किया जा सकता है, अगर हम को object बना कर किसी private member को access करे तो हमे error मिलेगा।

🐍
Example 3.py
Copy to clipboard
class Student:
    def __init__(self, name):
        self.__name = name   # private attribute

s = Student("Vikas")
print(s.__name) # error

Getter and Setter Function

private data members को direct access नहीं किया जा सकता, इसके लिए हम getter (data को access करने के लिए) और seetter (data को update करने के लिए ) function create करते है। 

हमने इस tutorial मे getter और setter function को simple में बनाया है ताकि आपको अच्छे से समझ आ जाए, आप चाहे तो इसे और complex और functional बना सकते है।

getter() function

getter function create करने के लिए हम getter या किसी और नाम से एक function define करेंगे जिसमे “ return self.__data_member “ code type करेंगे, यहाँ हमे self parameter लेना होगा और __data_member की जगह उस private data member का नाम होगा। अब हम उस private data member को access करने के लिए इस function को call करेंगे।

🐍
Example 4.py
Copy to clipboard
class Student:
    def __init__(self, name):
        self.__name = name   # private attribute

    def getter(self):  # create getter function
        return self.__name

s = Student("Vikas")
print(s.getter())  # getter calling

setter() function

setter function create करने के लिए हम setter या किसी और नाम से एक function define करेंगे जिसमे self और new_data parameter रहेगा, और इसके body में “ self.__data_member = new_data “ code type करेंगे, यहाँ __data_member की जगह उस private data member का नाम होगा और new_data में जो new data initialize करना चाहते है वो data रहेगा, हम कहते तो इसमे एक password parameter दे सकते है और if-else logic की help से password को check कर सकते है अगर match हुआ तो data member को modify होने देगा वरना error show करेगा, इससे हमारे program में और security मिलेगी। अब हम उस private data member को modify करने के लिए इस function को call करेंगे।

🐍
Example 5.py
Copy to clipboard
class Student:
    def __init__(self, name):
        self.__name = name   # private attribute

    def setter(self,new_data):  # create setter function
        self.__name = new_data

s = Student("Vikas")
s.setter("Lisa")  # setter calling

उम्मीद करते है कि आपको Encapsulation अच्छे समझ मे आ गया होगा। अपने learning को continue रखने के लिए next button पर click करे,

Add a comment...

  • All Posts
  • Artificial Intelligence
  • Computer Fundamentals
  • Computer Networks
  • Data Analytics
  • Data Science
  • DBMS
  • Deep Learning
  • Digital Fundamentals
  • DSA with Python
  • Excel
  • Exercise
  • Git & Github
  • Machine Learning
  • Matplotlib
  • Natural Language Processing
  • NumPy
  • Operating System
  • Pandas-s
  • Power BI
  • Python Tutorial
  • Scikit-learn
  • Seaborn
  • SQL & MySQL
Dunder Methods

Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods   Python Tutorial Python Introduction Identation &...

Encapsulation

Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers      2.1. Public Members      2.2. Protected Members...

Lambda Function

Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...

Nested Function

Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...

Context Manager

Table of Contents All Chapters 1. Context Manager 2. with Statement      2.1. __enter__() Method      2.2. __exit__()...

File Pointer

Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods      2.1. tell() Method      2.2....

File Handling

Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...

Edit Template
Scroll to Top