[Python-Dev] More on contextlib - adding back a contextmanager decorator

Phillip J. Eby pje at telecommunity.com
Mon May 1 06:17:18 CEST 2006


At 08:08 PM 4/30/2006 -0700, Guido van Rossum wrote:
>If you object against the extra typing, we'll first laugh at you
>(proposals that *only* shave a few characters of a common idiom aren't
>all that popular in these parts), and then suggest that you can spell
>foo.some_method() as foo().

Okay, you've moved me to at least +0 for dropping __context__.  I have only 
one object myself that has a non-self __context__, and it doesn't have a 
__call__, so none of my code breaks beyond the need to add parentheses in a 
few places.  ;)

As for decimal contexts, I'm thinking maybe we should have a 
decimal.using(ctx=None, **kw) function, where ctx defaults to the current 
decimal context, and the keyword arguments are used to make a modified 
copy, seems like a reasonable best way to implement the behavior that 
__context__ was added for.  And then all of the existing special machinery 
can go away and be replaced with a single @contextfactory.

(I think we should stick with @contextfactory as the decorator name, btw, 
even if we go back to calling __enter__/__exit__ things context managers.)



More information about the Python-Dev mailing list