[Python-Dev] Why are contexts also managers? (was r45544 - peps/trunk/pep-0343.txt)

Nick Coghlan ncoghlan at gmail.com
Thu Apr 20 13:49:43 CEST 2006


Phillip J. Eby wrote:
> At 11:41 PM 4/19/2006 +1000, Nick Coghlan wrote:
>> Given that naming though, I think contextlib.contextmanager should be 
>> renamed
>> to contextlib.context.
> 
> The name is actually correct under this terminology arrangement.  
> @contextmanager *returns* a context manager.

That's like saying we should describe the result of calling a generator 
function as a generator-iterable because it has an __iter__ method.

The longer name made sense when "context manager" was the term for the object 
with __enter__ and __exit__ methods (the way I originally wrote it when 
updating the PEP after the addition of the __context__ method).

While I agree that flipping the terminology the other way around makes sense, 
it *also* flips the effect of calling a decorated generator function so that 
what it returns is now appropriately termed a generator-context (since it 
provides __enter__ and __exit__ methods directly).

These contexts are context managers only in the same sense that iterators are 
also iterables. We shouldn't break the parallels between iterable-iterator and 
context manager-context without a good reason - and so far I haven't heard one.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list