
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Context Manager एक ऐसा mechanism है जिसमे resource management को easy बनाया जाता है जैसे file open and close करना, database connection manage करना, lock acquire and release करना, etc.
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 किया गया है।
with open("test.txt", "w") as f:
f.write("Hello World")
Context Manager दो special methods __enter__() और __exit__() का use करता है, जब हम “with” statement का use करते है तो python automatically इन दोनों methods को बारी-बारी call करता है।
जब “with” block start होता है तो सबसे पहले __enter__() method call होता है, “with” statement में इस method का काम setup/initialization का होता है, resource allocate करना, connection establish करना, etc. काम को __enter__() method करता है।
जब “with” block end होता है तो __exit__() method call होता है, “with” statement में इस method का काम cleanup & release का होता है, resource free करना, file close करना, etc. काम को __exit__() method करता है।
जैसा कि हम जान चुके है “with” statement दो special type के methods __enter__() और __exit__() का use करता है, इन दोनों methods को हम भी किसी logic पर define कर सकते है और इन दोनों methods को किसी class में use करके एक custom context manager बना सकते है।
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 करे,
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers 2.1. Public Members 2.2. Protected Members...
Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...
Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...
Table of Contents All Chapters 1. Parameters and Aguments 2. Function Parameters 2.1. *args 2.2. **kwargs...
Table of Contents All Chapters 1. Context Manager 2. with Statement 2.1. __enter__() Method 2.2. __exit__()...
Table of Contents All Chapters 1. Error Handling 2. Types of Error 2.1. Syntax Error 2.2....
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...