Understanding the Python with statement and context managers

The contextlib.contextmanager function decorator provides a handy way of providing a context manager without the need to write a full-fledged ContextManager class of your own (with __enter__ and __exit__ methods, so you don’t have to remember the arguments to the __exit__ method, or that the __exit__ method must return True in order to suppress the … Read more

Is Python *with* statement exactly equivalent to a try – (except) – finally block?

I’m going to put aside mentions of scope, because it’s really not very relevant. According to PEP 343, with EXPR as VAR: BLOCK translates to mgr = (EXPR) exit = type(mgr).__exit__ # Not calling it yet value = type(mgr).__enter__(mgr) exc = True try: try: VAR = value # Only if “as VAR” is present BLOCK … Read more

Python context manager that measures time

Other top-rated answers here can be incorrect As noted by @Mercury, the other top answer by @Vlad Bezden, while slick, is technically incorrect since the value yielded by t() is also potentially affected by code executed outside of the with statement. For example, if you run time.sleep(5) after the with statement but before the print … Read more

multiprocessing returns “too many open files” but using `with…as` fixes it. Why?

You’re creating new processes inside a loop, and then forgetting to close them once you’re done with them. As a result, there comes a point where you have too many open processes. This is a bad idea. You could fix this by using a context manager which automatically calls pool.terminate, or manually call pool.terminate yourself. … Read more

python ‘with’ statement, should I use contextlib.closing?

Yes, you should be using context.closing(); your own version does something different entirely. The with statement lets a context manager know when a block of code is entered and exited; on exit the context manager is also given access to the exception, if one occurred. File objects use this to automatically close the file when … Read more

Python nested context manager on multiple lines [duplicate]

Python 3.10 and newer Starting from Python 3.10, parentheses are allowed, and you can finally do this: with ( context1 as a, context2 as b ): pass Backslash characters Two or more physical lines may be joined into logical lines using backslash characters (\) (citing the Explicit line joining section) If you want put context … Read more

Return value of __exit__

Yes, that return statement is redundant. Only when type is not None does the return value matter. From the object.__exit__() documentation: If an exception is supplied, and the method wishes to suppress the exception (i.e., prevent it from being propagated), it should return a true value. Otherwise, the exception will be processed normally upon exit … Read more

Are multiple `with` statements on one line equivalent to nested `with` statements, in python?

Yes, listing multiple with statements on one line is exactly the same as nesting them, according to the Python 2.7 language reference: With more than one item, the context managers are processed as if multiple with statements were nested: with A() as a, B() as b: suite is equivalent to with A() as a: with … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)