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

Paul Moore p.f.moore at gmail.com
Mon Apr 24 22:35:01 CEST 2006


The current, alpha 2, documentation insists that objects with
__enter__ and __exit__ methods must also define __context__ in such a
way that it returns self.

I don't understand why that is necessary.

I can understand that it is convenient, in cases where __context__
doesn't need to create a new object each time, but is it *necessary*?

Specifically, is there a use case where you need to say "with x" where
x is the return value of a __context__ method, or where you call
__context__ on something you got from __context__? I can't find one in
the PEP or in the code for contextlib...

By insisting that things with __enter__ and __exit__ methods must
implement __context__, there's a subtype relationship which I *think*
means that Nick's insistence that the concepts are distinct, becomes
difficult to support. But the terms are so confused now, that I'm
utterly unable to frame my objection clearly.

Can someone clarify this?

Paul.


More information about the Python-Dev mailing list