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

Brendan Barnwell brenbarn at brenbarn.net
Sun Nov 6 02:35:31 EST 2016


On 2016-11-06 00:18, 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?

	It's weird to not want the __exit__ to be called if it's defined as a 
finally block, which is what you're doing with the way you're using 
contextlib.contextmanager.  The __exit__ block there is effectively 
whatever is after the yield in the generator function you write.  If 
there's code you don't want to always be run, don't put that code inside 
a finally where the yield is in the try.

-- 
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is no 
path, and leave a trail."
    --author unknown


More information about the Python-ideas mailing list