Re: [Python-Dev] Proposed changes to PEP 343
Nick Coghlan did a +1 job to write:
1. Amend the statement specification such that:
with EXPR as VAR: BLOCK
is translated as:
abc = (EXPR).__with__() exc = (None, None, None) VAR = abc.__enter__() try: try: BLOCK except: exc = sys.exc_info() raise finally: abc.__exit__(*exc)
Note that __with__ and __enter__ could be combined into one with no loss of functionality: abc,VAR = (EXPR).__with__() exc = (None, None, None) try: try: BLOCK except: exc = sys.exc_info() raise finally: abc.__exit__(*exc) - Anders
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. It would be like trying to combine __iter__() and next() into the same method for iterators. . . Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://boredomandlaziness.blogspot.com
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. Strictly speaking there's no guarantee that the type of 'abc' is known to the author of __with__, but I can't imagine an example where that would not be the case.
It would be like trying to combine __iter__() and next() into the same method for iterators. . .
The with-statement needs two pieces of information from the expression: Which object to bind to the users's variable (VAR) and which object takes care of block-exit cleanup (abc). A combined method would give these two equal standing rather than deriving one from the other. Nothing ugly about that. - Anders
On 10/9/05, Anders J. Munch <andersjm@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/)
participants (4)
-
Anders J. Munch -
Anders J. Munch -
Guido van Rossum -
Nick Coghlan