About 175,000 results
Open links in new tab
  1. Understanding the Python with statement and context managers

    Understanding the Python with statement and context managers Asked 15 years, 3 months ago Modified 1 year, 1 month ago Viewed 20k times

  2. What is the purpose of a context manager in python

    What is the purpose of a context manager in python [duplicate] Asked 9 years, 8 months ago Modified 7 years, 5 months ago Viewed 7k times

  3. python - Using context managers without "with" block - Stack Overflow

    Feb 3, 2018 · Files are already context managers (which close themselves in __exit__). So make header_file a normal function that returns an open file to which it has written a header line. Then you …

  4. python - Context Managers as a class vs. function? - Stack Overflow

    The way using @contextmanager and allowing one to declare context managers as functions is just a practical utility in Python's standard library. What the decorator yields is an object that have both …

  5. Writing a context manager in Python that itself uses a with statement

    Jul 12, 2012 · My use case is a login context manager for testing a view (in a webapp). Logging in requires a couple of calls to mock, which occur via a with statement. I want users of this helper logIn …

  6. Explaining Python's '__enter__' and '__exit__' - Stack Overflow

    Writing a class-based context manager isn’t the only way to support the with statement in Python. The contextlib utility module in the standard library provides a few more abstractions built on top of the …

  7. python - How does open () work with and without `with`? - Stack …

    Apr 3, 2014 · The problem is that contextmanager only provides exactly that; a context manager to be used in the with statement. Calling the function does not return the file object, but a special context …

  8. python - Handling instances of a context manager inside another …

    47 How should a context manager created inside another context manager be handled in Python? Example: suppose you have class A that acts as a context manager, and class B that also acts as a …

  9. Why can you use open() as context manager? - Stack Overflow

    May 3, 2017 · From Python's source code of open, I think open is just a normal function. Why can we use it like below? with open ('what_are_context_managers.txt', 'r') as infile: for line in infile:

  10. python - How to safely handle an exception inside a context manager ...

    The __exit__ method is called as normal if the context manager is broken by an exception. In fact, the parameters passed to __exit__ all have to do with handling this case! From the docs: …