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

Phillip J. Eby pje at telecommunity.com
Tue Apr 25 20:59:10 CEST 2006


At 11:37 AM 4/25/2006 -0700, Guido van Rossum wrote:
>But what's the use case? Have we actually got an example where it
>makes sense to use the "thing with __enter__ and __exit__ methods" in
>a with-statement, other than the (many) examples where the original
>__context__ method returns "self"?

Objects returned by @contextfactory-decorated functions must have __enter__ 
and __exit__ (so @contextfactory can be used to define __context__ methods) 
*and* they must also have __context__, so they can be used directly in a 
"with" statement.

I think that in all cases where you want this (enter/exit implies context 
method availability), it's going to be the case that you want the context 
method to return self, just as iterating an object with a next() method 
normally returns that object.



More information about the Python-Dev mailing list