Table of Contents
Python Tutorial
Table of Contents
Python Tutorial
Context Manager in Hindi
What is Context Manager in Python
Context Manager एक ऐसा mechanism है जिसमे resource management को easy बनाया जाता है जैसे file open and close करना, database connection manage करना, lock acquire and release करना, etc.
with Statement
Python में Context manager mostly “with” statement के साथ use होता है। “with” block जब start होता है तो resource allocate होता है, और जब “with” block खतम होता है तो resource release हो जाता है, भले ही block के अंदर के code में कोई error ही क्यों न आया हो, फिर भी resource release हो जाएगा।
For example हमने example 1 में एक file को “with” के साथ open किया है और इसे close करने के लिए close() method का use नहीं किया है फिर भी ये file close हो जाएगा क्योंकि यहाँ पर context manager (“with” statement) का use किया गया है।
How Context Manager (“with” Statement) Works Internally
Context Manager दो special methods __enter__() और __exit__() का use करता है, जब हम “with” statement का use करते है तो python automatically इन दोनों methods को बारी-बारी call करता है।
__enter__() Method
जब “with” block start होता है तो सबसे पहले __enter__() method call होता है, “with” statement में इस method का काम setup/initialization का होता है, resource allocate करना, connection establish करना, etc. काम को __enter__() method करता है।
__exit__() Method
जब “with” block end होता है तो __exit__() method call होता है, “with” statement में इस method का काम cleanup & release का होता है, resource free करना, file close करना, etc. काम को __exit__() method करता है।
Custom Context Manager
जैसा कि हम जान चुके है “with” statement दो special type के methods __enter__() और __exit__() का use करता है, इन दोनों methods को हम भी किसी logic पर define कर सकते है और इन दोनों methods को किसी class में use करके एक custom context manager बना सकते है।
उम्मीद करते है कि आपको Context Manager अच्छे समझ मे आ गया होगा। अपने 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...