Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.106 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.79; '*S*': 0.00; 'logic': 0.09; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'nesting': 0.16; 'programmer,': 0.16; 'wayne': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'this?': 0.23; 'mon,': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'moved': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'checking': 0.33; 'skip:d 20': 0.34; "can't": 0.35; 'transaction': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'level': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'track': 0.38; 'extremely': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'simply': 0.61; 'first': 0.61; 'back': 0.62; 'jul': 0.74; 'ethan': 0.84; 'furman': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=AbZmt4aveKcoC6HLqzjRUt0fo+menYc/wKsI6BED9Ic=; b=kWjzXYE6gi9jJIOfUJxWQcAEJdt9/SetyhxnramnGJqW3khJH40QE3Ca1MB7bRMKA0 pvBwdQ6hAL828ULDcWCLSx2rRRqacs3yFwdx6AtI/JWWu939464PH5PUFGAKcQcnDFS+ YFg0FWyc9XvHUsfGlIeuFlXKBWow6eQud5+/U8tTBVrrve1/XjZwmuJvdQqgy3cCNv/v +zBvGSVeFcb+nAKIZIPywPBtaTO3YS16IuFMwvTf2LU+palTcBhagOKGCeXmgmOdRRhP zdDvbNjFCEgIoBpZ4oFAbFEPh1/S44KFOViF9YQQk9tmB4DeP9AoWG+aufYNpDw1FY1b JOvQ== MIME-Version: 1.0 X-Received: by 10.52.34.40 with SMTP id w8mr10555981vdi.7.1373244483784; Sun, 07 Jul 2013 17:48:03 -0700 (PDT) In-Reply-To: <51D99CBC.40606@stoneleaf.us> References: <51d4eb9c$0$29999$c3e8da3$5496439d@news.astraweb.com> <51d508ed$0$6512$c3e8da3$5496439d@news.astraweb.com> <51d5a504$0$29999$c3e8da3$5496439d@news.astraweb.com> <51D99CBC.40606@stoneleaf.us> Date: Mon, 8 Jul 2013 10:48:03 +1000 Subject: Re: Default scope of variables From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373244492 news.xs4all.nl 15932 [2001:888:2000:d::a6]:38013 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50112 On Mon, Jul 8, 2013 at 2:52 AM, Ethan Furman wrote: > On 07/07/2013 06:43 AM, Chris Angelico wrote: >> >> On Sun, Jul 7, 2013 at 11:13 PM, Wayne Werner >> 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