Create a “with” block on several context managers? [duplicate]

This question already has answers here: Multiple variables in a ‘with’ statement? (8 answers) Closed 3 years ago. Suppose you have three objects you acquire via context manager, for instance A lock, a db connection and an ip socket. You can acquire them by: with lock: with db_con: with socket: #do stuff But is there … Read more

Are there legitimate uses for JavaScript’s “with” statement?

Alan Storm’s comments in response to my answer regarding the with statement got me thinking. I’ve seldom found a reason to use this particular language feature, and had never given much thought to how it might cause trouble. Now, I’m curious as to how I might make effective use of with, while avoiding its pitfalls. … Read more

Explaining Python’s ‘__enter__’ and ‘__exit__’

I saw this in someone’s code. What does it mean? def __enter__(self): return self def __exit__(self, type, value, tb): self.stream.close() from __future__ import with_statement#for python2.5 class a(object): def __enter__(self): print ‘sss’ return ‘sss111’ def __exit__(self ,type, value, traceback): print ‘ok’ return False with a() as s: print s print s 7 s 7 Using these … Read more