Default scope of variables

Ethan Furman ethan at stoneleaf.us
Sun Jul 7 18:52:12 CEST 2013


On 07/07/2013 06:43 AM, Chris Angelico wrote:
> 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:

Is there some reason you can't simply do this?

  with new_transaction(conn) as tran1:
       tran1.query("blah")
       with tran1.subtransaction() as tran2:
           tran2.query("blah")
           with tran2.subtransaction() as tran3:
               tran3.query("blah")
               # roll this subtransaction back
           tran2.query("blah")
           tran2.commit()
       tran1.query("blah")
       tran1.commit()

--
~Ethan~



More information about the Python-list mailing list