[Python-Dev] With context, please (original) (raw)

Phillip J. Eby pje at telecommunity.com
Sun Apr 23 08:03:40 CEST 2006


At 01:33 PM 4/23/2006 +1000, Nick Coghlan wrote:

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. 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 :).

Read the first sentence again:

"EXPRESSION returns a value that the with statement uses to create a context" (emphasis added).

It doesn't say that the value is the context, and if anything, the second excerpt supports that by implying that the context manager is the thing passed to the "with" statement.

I could be wrong, Nick, but it really looks to me like you're the only person who's gotten this particular interpretation, and I at least don't understand what the supporting argument for your interpretation is, other than that it's what you always meant it to be. (Whereas there are obvious rationales for context returning a "context" object, and for a context manager being longer lived than contexts.)

I also haven't seen you explain your theory of why you should be able to take the object returned from context and pass it into the "with" statement.

Given those things, I'd suggest that the consensus is overwhelmingly in favor of the "with contextmanager" terminology, or something similar, and that should be what gets used. I don't think it's fair to say that there's a lack of consensus in the sense that it could go either way like a 50-50 or 60-40. So far, it's 5:1 against the object passed to "with" being called the "context", even if Guido abstains.

Also, there haven't been any complaints of the a1 documentation being unclear on this; rather, it was the PEP that was considered confusing. So in effect, we already tested this in a1, with one interpretation in the docs and another in the PEP, and the documenation won.



More information about the Python-Dev mailing list