Home >> Python Tutorial >> Context Manager
Edit Template
Edit Template

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 किया गया है।

🐍
Example 1.py
Copy to clipboard
with open("test.txt", "w") as f:
    f.write("Hello World")

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 बना सकते है।

🐍
Example 4.py
Copy to clipboard
class MyContext:
    def __enter__(self):
        print("Resource allocated (Enter)")
        return "Data from context"

    def __exit__(self, exc_type, exc_value, traceback):
        print("Resource released (Exit)")

# Use with `with`
with MyContext() as value:
    print("Inside block ->", value)

उम्मीद करते है कि आपको Context Manager अच्छे समझ मे आ गया होगा। अपने 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