[Python-Dev] Proposed changes to PEP 343

Guido van Rossum guido at python.org
Sun Oct 9 16:28:29 CEST 2005


On 10/9/05, Anders J. Munch <andersjm at inbound.dk> wrote:
> Nick Coghlan wrote:
>  >Anders J. Munch wrote:
>  >
>  >>Note that __with__ and __enter__ could be combined into one with no
>  >>loss of functionality:
>  >>
>  >>        abc,VAR = (EXPR).__with__()
>  >>
>  >
>  >They can't be combined, because they're invoked on different objects.
>  >
>
> Sure they can.  The combined method first does what __with__ would
> have done to create abc, and then does whatever abc.__enter__ would
> have done.  Since the type of 'abc' is always known to the author of
> __with__, this is trivial.

I'm sure it can be done, but I find this ugly API design. While I'm
not keen on complicating the API, the decimal context example has
convinced me that it's necessary. The separation into __with__ which
asks EXPR for a context manager and __enter__ / __exit__ which handle
try/finally feels right. An API returning a tuple is asking for bugs.

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


More information about the Python-Dev mailing list