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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'represents': 0.05; 'c++,': 0.07; 'postgresql': 0.07; 'back.': 0.09; 'happen,': 0.09; 'python': 0.11; '(there': 0.16; '__del__': 0.16; 'brace': 0.16; 'called,': 0.16; 'fails.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'garbage': 0.16; 'guarded': 0.16; 'applies': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'example': 0.22; 'entries': 0.24; 'mention': 0.26; 'gets': 0.27; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; '(this': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'class': 0.32; 'fri,': 0.33; 'transaction': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'c++': 0.36; 'doing': 0.36; 'project': 0.37; 'being': 0.38; 'actions': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'transaction.': 0.60; 'skip:n 10': 0.64; 'finally': 0.65; 'close': 0.67; 'jul': 0.74; 'guaranteed': 0.75; 'power': 0.76; 'back?': 0.84; 'rolls': 0.84; 'expires': 0.91; '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=MduSZOqx+7F4VwOyo92cI62DsS33PnF6U0zY2b2xsJk=; b=M05BsNcg0A42LnIoqMVxupDN+x/Djiv2mLTL6/BAgL81UZcFfoIfDQzTXCt5QtU/bY 7R06IxJPuLnzML6I/bYg9gBkgpmsblYb8iJUe3lS6BwtJ0gl60cxm56Gu/PiP9HEsR2I x60TD+fyFzikaVoXfA4it94H5deLkto2Z6t6iG/lR7QTDEi6mH50Xayke1Et6ZzJBwF4 gGjbSHes3a/Gj6duTX3wwH224fMGDmxJkfBnRywZ08DfBsHUvIo1ae9mkkdlopSLBRr/ 1SyRGJv/QBicWLJz9oq/v1qV4FKORqKhWrzDVIH0YPFRPtQI7qjIrRvotyyikgnuATwG lSTg== MIME-Version: 1.0 X-Received: by 10.220.128.72 with SMTP id j8mr3620101vcs.3.1372957131102; Thu, 04 Jul 2013 09:58:51 -0700 (PDT) In-Reply-To: <51d5a504$0$29999$c3e8da3$5496439d@news.astraweb.com> 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> Date: Fri, 5 Jul 2013 02:58:50 +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: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372957141 news.xs4all.nl 15958 [2001:888:2000:d::a6]:46661 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49887 On Fri, Jul 5, 2013 at 2:38 AM, Steven D'Aprano wrote: > On Thu, 04 Jul 2013 15:47:57 +1000, Chris Angelico wrote: >> Here's one example of shadowing that comes from a C++ project at work. I >> have a class that represents a database transaction (constructing it >> begins a transaction, it has methods for doing queries, and its >> destructor rolls back). > > When the object finally gets garbage collected, doesn't that mean the > last transaction will be rolled back? 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. And since this is PostgreSQL we use, the same applies if the process is SIGKILLed or the power fails. If commit() doesn't happen, neither does the transaction. (There are a few actions the program can take that are deliberately non-transactional - log entries of various sorts, mainly - but everything else is guarded in this way.) ChrisA