[Python-Dev] Must objects with __enter__/__exit__ also supply __context__?

Phillip J. Eby pje at telecommunity.com
Wed Apr 26 02:19:04 CEST 2006


At 04:18 PM 4/25/2006 -0700, Guido van Rossum wrote:
>But the question remains,
>under what circumstances is it convenient to call __context__()
>explicit, and pass the result to a with-statement?

Oh.  I don't know of any; I previously asked the same question myself.  I 
just eventually answered myself with "I don't care; we need to require 
self-returning __context__ on execution context objects so that the 
documentation can appear vaguely sane."  :)

So, I don't know of any non-self-returning use cases for __context__ on an 
object that has __enter__ and __exit__.  In fact, I suspect that we could 
strengthen the requirements to say that:

1. If you have __enter__ and __exit__, you MUST have a self-returning 
__context__

2. If you don't have __enter__ and __exit__, you MUST NOT have a 
self-returning __context__

#2 is obvious since you can't use the object with "with" otherwise.  #1 
reflects the fact that it doesn't make any sense to take the context of a 
context.



More information about the Python-Dev mailing list