Default scope of variables
Chris Angelico
rosuav at gmail.com
Sun Jul 7 09:43:24 EDT 2013
On Sun, Jul 7, 2013 at 11:13 PM, Wayne Werner <wayne at waynewerner.com> wrote:
> Which you would then use like:
>
>
> conn = create_conn()
> with new_transaction(conn) as tran:
> rows_affected = do_query_stuff(tran)
> if rows_affected == 42:
> tran.commit()
Yep. There's a problem, though, when you bring in subtransactions. The
logic wants to be like this:
with new_transaction(conn) as tran:
tran.query("blah")
with tran.subtransaction() as tran:
tran.query("blah")
with tran.subtransaction() as tran:
tran.query("blah")
# roll this subtransaction back
tran.query("blah")
tran.commit()
tran.query("blah")
tran.commit()
The 'with' statement doesn't allow this. I would need to use some kind
of magic to rebind the old transaction to the name, or else use a list
that gets magically populated:
with new_transaction(conn) as tran:
tran[-1].query("blah")
with subtransaction(tran):
tran[-1].query("blah")
with subtransaction(tran):
tran[-1].query("blah")
# roll this subtransaction back
tran[-1].query("blah")
tran[-1].commit()
tran[-1].query("blah")
tran[-1].commit()
I don't like the look of this. It might work, but it's hardly ideal.
This is why I like to be able to nest usages of the same name.
ChrisA
More information about the Python-list
mailing list