Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python,': 0.02; 'c++,': 0.07; 'context': 0.07; 'duplicate': 0.07; 'back.': 0.09; 'executed': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '__del__': 0.16; 'brace': 0.16; 'called,': 0.16; 'handling,': 0.16; 'tran': 0.16; 'wayne': 0.16; 'wrote:': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'class.': 0.26; 'mention': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; '(this': 0.29; 'skip:@ 10': 0.30; 'code': 0.31; 'block,': 0.31; 'fri,': 0.33; 'skip:d 20': 0.34; 'could': 0.34; 'transaction': 0.35; 'something': 0.35; 'but': 0.35; 'yield': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'being': 0.38; 'manager': 0.38; 'does': 0.39; 'called': 0.40; 'managers': 0.61; 'simple': 0.61; 'guarantee': 0.63; 'kind': 0.63; 'to:addr:gmail.com': 0.65; 'close': 0.67; 'jul': 0.74; 'guaranteed': 0.75; 'behavior': 0.77; 'as:': 0.81; 'end.': 0.84; '2013,': 0.91; 'expires': 0.91 Date: Sun, 7 Jul 2013 08:13:43 -0500 (CDT) From: Wayne Werner X-X-Sender: wayne@gilgamesh To: Chris Angelico Subject: Re: Default scope of variables In-Reply-To: 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> User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Cc: python-list@python.org 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373202876 news.xs4all.nl 15959 [2001:888:2000:d::a6]:40696 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50101 On Fri, 5 Jul 2013, Chris Angelico wrote: > Oh. Uhm... ahh... it would have helped to mention that it also has a > commit() method! But yes, that's correct; if the object expires (this > is C++, so it's guaranteed to call the destructor at that close brace > - none of the Python vagueness about when __del__ is called) without > commit() being called, then the transaction will be rolled back. If one wants to duplicate this kind of behavior in Python, that's what context managers are for combined with a `with` block, which does guarantee that the __exit__ method will be called - in this case it could be something as simple as: from contextlib import contextmanager @contextmanager def new_transaction(conn): tran = conn.begin_transaction() yield tran if not tran.committed: tran.rollback() 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() And then you get the desired constructor/destructor behavior of having guaranteed that code will be executed at the start and at the end. You can wrap things in try/catch for some error handling, or write your own context manager class. HTH, Wayne