[Python-Dev] With context, please

Nick Coghlan ncoghlan at gmail.com
Sun Apr 23 05:33:26 CEST 2006


Phillip J. Eby wrote:
> At 09:25 AM 4/22/2006 -0700, Aahz wrote:
>>     EXPRESSION returns a value that the with statement uses to create a
>>     context (a special kind of namespace).  The context is used to
>>     execute the BLOCK.  The block might end normally, get terminated by
>>     a break or return, or raise an exception. No matter which of those
>>     things happens, the context contains code to clean up after the
>>     block.
>>
>>     The as NAME part is optional.  If you include it, you can use NAME
>>     in your BLOCK
>>
>> Then a bit later:
>>
>>     The protocol used by the with statement is called the context
>>     management protocol, and objects implementing it are context
>>     managers.
> 
> Okay, which means that you agree with AMK and Paul Moore that the thing you 
> pass to "with" is a context manager, and the thing that controls execution 
> is a context.  Was that conclusion independently arrived at, or based on 
> reading e.g. the docs I wrote?  Obviously, if you guys came up with that 
> terminology on your own, that's a stronger vote in its favor.

I think I've figured out where you and I went off in different directions with 
this - when you read "context management protocol" in the PEP you interpreted 
it as "has a __context__ method that produces an object with 
__enter__/__exit__ methods", but when I originally added the term "context 
management protocol" to the PEP what I actually meant was "has 
__enter__/__exit__ methods and a __context__ method that returns self" (the 
last part of that definition being added implicitly when the __context__ 
method was introduced). Starting from that point, I'm no longer surprised you 
considered the PEP to be inconsistent in its use of the terminology :)

As far as I can tell, Aahz's book doesn't currently say anything that favours 
one interpretation over the other (which is probably a good thing from Aahz's 
point of view :).

In case its not entirely clear why I think Aahz's wording is neutral, it's 
because in my intended interpretation the context manager sets up and tears 
down an abstract execution context which is distinct from the concrete context 
object that provided the manager in the first place. The manager is a mediator 
that translates from the data passively recorded in the context object to the 
appropriate active manipulations of the runtime state.

I realise the overloading of the term 'context' is potentially confusing, and 
I didn't clearly acknowledge the distinction myself until the recent 
discussion. Not acknowledging that distinction appears to have been largely 
responsible for my manifest failure to document this properly in the PEP. . .

Cheers,
Nick.

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


More information about the Python-Dev mailing list