On Feb 02, 2010, at 06:21 PM, Jeroen Vermeulen wrote:

>(Irrelevant gripes: it's a thin layer of sugar;

True, but it's a tasty bit of sugar.  I find that carefully used,
with-statements are more readable than try/finallys.

>it doesn't scale well for more than resource;
'one'?------------------------------^

It's awkward to do in Python 2.6, but possible:

    >>> from contextlib import nested
    >>> with nested(open('/etc/passwd'), open('/etc/group')) as (a, b):
    ...     pass

But easier in Python 2.7 and 3:

    >>> with open('/etc/passwd') as a, open('/etc/group') as b:
    ...     pass


>The justification for this design was transactions: it lets you write 
>transaction classes don't need explicit commits or aborts, because they 
>can see for themselves if the code block exits normally or with an 
>exception.

I'm not sure the with-statement was designed for transactions, but one good
reason why it shouldn't be used for transactions is because (IME) proper
handling doesn't fit into pure try/finally:

    txn = transaction.begin()
    try:
        modify_the_database()
    except:
        # Yes, bare except.
        txn.abort()
    else:
        # Ah, everything succeeded.
        txn.commit()

That's not a good fit for 'with'.  I suppose you can wedge it into the context
manager protocol, but it wouldn't be the first thing I'd reach for.

I guess that means we (mostly) agree! :)
-Barry

Attachment: signature.asc
Description: PGP signature

_______________________________________________
Mailing list: https://launchpad.net/~launchpad-dev
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~launchpad-dev
More help   : https://help.launchpad.net/ListHelp

Reply via email to