Default scope of variables

Chris Angelico rosuav at gmail.com
Sun Jul 7 20:48:03 EDT 2013


On Mon, Jul 8, 2013 at 2:52 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> 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()

That means that I, as programmer, have to keep track of the nesting
level of subtransactions. Extremely ugly. A line of code can't be
moved around without first checking which transaction object to work
with.

ChrisA



More information about the Python-list mailing list