Table of Contents
Python Tutorial
Table of Contents
Python Tutorial
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 कर सकते है।
Protected Members
जब हम किसी data members (variables, methods, etc.) के name के आगे single underscore “_” लगते है तो वो protected member बन जाता है, protected members को सिर्फ class और subclass में ही access किया जा सकता है, outside में इसे access नहीं किया जा सकता है।
Private Members
जब हम किसी data members (variables, methods, etc.) के name के आगे double underscore “__” लगते है तो वो private member बन जाता है, private member को class के बाहर access नहीं किया जा सकता है, अगर हम को object बना कर किसी private member को access करे तो हमे 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 करेंगे।
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 करेंगे।
उम्मीद करते है कि आपको Encapsulation अच्छे समझ मे आ गया होगा। अपने learning को continue रखने के लिए next button पर click करे,
- All Posts
- Artificial Intelligence
- Computer Fundamentals
- Computer Networks
- Data Analytics
- Data Science
- DBMS
- Deep Learning
- Digital Electronics
- 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

Table of Contents All Chapters 1. Personal Area Network 2. Local Area Network 3. Campus Area Network 4. Metropolitan Area...

Table of Contents All Chapters 1. Data Communication 2. Types of Data Communication 2.1. Simplex 2.2....

Table of Contents All Chapters 1. Special Logic Gate 2. XOR Gate 2.1. XOR Gate Truth Table 3....

Table of Contents All Chapters 1. Universal Logic Gates 2. NOR Gate 2.1. NOR Gate Truth Table 3....

Table of Contents All Chapters 1. Logic Gate 2. AND Gate 2.1. AND Gate Truth Table 3. OR...

Table of Contents All Chapters 1. Simplification 2. Simplification using Boolean Algebra Theorems 3. Simplification using K-map Digital Electronics...

Table of Contents All Chapters 1. Identity Laws 2. Null Laws 3. Domination Laws 4. Idempotent Laws 5. Complement Laws...