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.030 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'method.': 0.07; 'bug.': 0.09; 'invalidate': 0.09; 'occurrences': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'typed': 0.09; 'wrote': 0.14; 'attributes,': 0.16; 'concern,': 0.16; 'objection': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 70': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'commit': 0.19; 'code,': 0.22; 'mind.': 0.24; 'looks': 0.24; 'script': 0.25; 'class.': 0.26; 'header:X-Complaints-To:1': 0.27; 'raise': 0.29; "doesn't": 0.30; 'code': 0.31; '(unless': 0.31; 'concern.': 0.31; 'once,': 0.31; 'class': 0.32; 'there.': 0.32; 'this.': 0.32; 'checked': 0.32; 'another': 0.32; 'quite': 0.32; 'actual': 0.34; 'received:co.za': 0.34; 'received:za': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'basic': 0.35; 'transaction': 0.35; 'objects': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'are,': 0.36; 'method': 0.36; 'thanks': 0.36; 'possible': 0.36; 'error.': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'reviewed': 0.60; 'new': 0.61; 'here': 0.66; 'answer.': 0.68; 'frank': 0.68; 'received:41': 0.70; 'brand': 0.72; 'jul': 0.74; 'activated': 0.84; 'dry': 0.84; 'ethan': 0.84; 'front.': 0.84; 'mistake': 0.91; 'mistakenly': 0.91; 'technique': 0.93; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: Default scope of variables Date: Thu, 11 Jul 2013 07:52:32 +0200 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> <51DC355E.20606@stoneleaf.us> X-Gmane-NNTP-Posting-Host: 41-135-99-227.dsl.mweb.co.za X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.4657 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4913 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: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373522000 news.xs4all.nl 16010 [2001:888:2000:d::a6]:60864 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50416 "Ian Kelly" wrote in message news:CALwzidk2+b5bYM5B+XVtoZ8LHeyVHcos4v58F8Z2o1Jb6sAzog@mail.gmail.com... > On Tue, Jul 9, 2013 at 11:54 PM, Frank Millman wrote: >> You had me worried there for a moment, as that is obviously an error. >> >> Then I checked my actual code, and I find that I mis-transcribed it. It >> actually looks like this - >> >> with db_session as conn: >> db_session.transaction_active = True >> conn.cur.execute(...) >> >> I am still not quite sure what your objection is to this. It feels >> straightforward to me. >> >> Here is one possible answer. Whenever I want to commit a transaction I >> have >> to add the extra line. There is a danger that I could mis-spell >> 'transaction_active', in which case it would not raise an error, but >> would >> not commit the transaction, which could be a hard-to-trace bug. Using >> your >> approach, if I mis-spelled 'db_session.connect()', it would immediately >> raise an error. >> >> Is that your concern, or are there other issues? > > Yes, that is one concern. Another is that since you mistakenly typed > "conn" instead of "db_session" once, you might make the same mistake > again in actual code, with the same effect (unless the conn object > doesn't allow arbitrary attributes, which is a possibility). Another > is that the code adheres better to the DRY principle if you don't need > to copy that line all over the place. Thanks to you and Ethan - that does make sense. I have reviewed my code base to see how many occurrences there are, and there are just three. All database objects inherit from a DbOject class. The class has a save() method and a delete() method. Each of these requires a commit, so I use my technique there. All database updates are activated by calling save() or delete(). I have an init.py script to 'bootstrap' a brand new installation, which requires populating an empty database with some basic structures up front. DbObject cannot be used here as the required plumbing is not in place, so I use my technique here as well. However, this does not invalidate your general point, so I will keep it in mind. Thanks Frank