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

Guido van Rossum guido at python.org
Wed Apr 26 02:20:17 CEST 2006


On 4/25/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> 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.

I would augment #1 to clarify that if you have __enter__ and __exit__
you may not have __context__ at all; if you have all three,
__context__ must return self.

#2 is unnecessary (already covered elsewhere).

--
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list