2

I am aware that the with statement allows context managers to 'clean up after themselves', but other than the well known

with open("text.txt") as f:
    data = f.read()

what uses does the with statement have utilizing just the standard library, without creating custom context managers?

enigma
  • 3,476
  • 2
  • 17
  • 30

1 Answers1

1

At the very least, it's useful in Python threading (although geez, Python threading ain't ever that useful). https://docs.python.org/2/library/threading.html#using-locks-conditions-and-semaphores-in-the-with-statement

Oh, and pool executors.

with concurrent.futures.ProcessPoolExecutor(max_workers=10) as executor:
    for _ in range(10):
        executor.submit(some_func)
jwilner
  • 6,348
  • 6
  • 35
  • 47