[Python-Dev] Proposed changes to PEP 343

Anders J. Munch andersjm at inbound.dk
Sun Oct 9 16:00:04 CEST 2005


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



More information about the Python-Dev mailing list