[Python-ideas] Generator-based context managers can't skip __exit__

Terry Reedy tjreedy at udel.edu
Sun Nov 6 09:40:09 EST 2016


On 11/6/2016 2:18 AM, Ram Rachum wrote:

> Well, you think it's weird that I want a `finally` clause to not be
> called in some circumstances. Do you think it's equally weird to want an
> `__exit__` method that is not called in some circumstances?

Without a deeper understanding of why you want to do so, I would.  The 
automatic exit cleanup typically the main purpose of a context manager 
and 'with' block.  If, for instance, I want a file only maybe closed, I 
would just call 'open' instead of 'with open' and then conditionally 
(with 'if' or 'try') call 'close'.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list