Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'class,': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; 'loop.': 0.16; 'magic': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'statement': 0.30; 'code': 0.31; 'this.': 0.32; 'option': 0.32; 'sense': 0.34; 'transaction': 0.35; 'charset:us-ascii': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:173': 0.61; 'back': 0.62; 'kind': 0.63; 'skip:n 10': 0.64; 'more': 0.64; "'with'": 0.84 Date: Sun, 07 Jul 2013 11:59:28 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Default scope of variables 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> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.233]) [173.12.184.233]:50043 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373224898 news.xs4all.nl 15957 [2001:888:2000:d::a6]:57526 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50108 On 07/07/2013 06:43 AM, Chris Angelico wrote: > > 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() The other option is to build the magic into the new_transaction class, then your code will look like: with new_transaction(conn) as tran: tran.query("blah") with tran.subtransaction(): tran.query("blah") with tran.subtransaction(): tran.query("blah") # roll this subtransaction back tran.query("blah") tran.commit() tran.query("blah") tran.commit() This would definitely make more sense in a loop. ;) -- ~Ethan~