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 करे,

Add a comment...

  • 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
computer networks

Table of Contents All Chapters 1. OSI Model      1.1. Physical Layer      1.2. Data Link Layer  ...

computer networks

Table of Contents All Chapters 1. Topology 2. Types of Topology      2.1. Point to Point Topology    ...

computer networks

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

digital electronics number system

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

Edit Template

Sign up to track your learning

Login to continue your learning tracking

To reset your password, please enter your email address or username below.

Scroll to Top