Path: csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'attribute': 0.07; 'bug.': 0.09; '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; 'def': 0.12; 'wrote': 0.14; 'concern,': 0.16; 'enough.': 0.16; 'objection': 0.16; 'received:69.93': 0.16; 'received:gateway13.websitewelcome.com': 0.16; 'skip:n 70': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'obviously': 0.18; 'commit': 0.19; 'work,': 0.20; 'seems': 0.21; '>>>': 0.22; 'code,': 0.22; 'example': 0.22; 'header:User-Agent:1': 0.23; 'looks': 0.24; "i've": 0.25; 'source': 0.25; 'this:': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'raise': 0.29; "doesn't": 0.30; '(which': 0.31; 'code': 0.31; 'concern': 0.31; 'class': 0.32; 'this.': 0.32; 'checked': 0.32; 'another': 0.32; 'quite': 0.32; 'actual': 0.34; 'noticed': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'connection': 0.35; 'possible.': 0.35; 'transaction': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'version': 0.36; 'consistent': 0.36; 'charset:us-ascii': 0.36; 'possible': 0.36; 'error.': 0.37; 'saves': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'skip:u 10': 0.60; 'received:173': 0.61; 'places': 0.64; 'here': 0.66; 'answer.': 0.68; 'frank': 0.68; 'jul': 0.74; 'bitten': 0.84; 'demonstrates': 0.84; 'ethan': 0.84; 'factoring': 0.84; 'furman': 0.84; '2013': 0.98 Date: Wed, 10 Jul 2013 08:29:24 -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> <51DC355E.20606@stoneleaf.us> 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]:35753 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 3 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: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373471632 news.xs4all.nl 15913 [2001:888:2000:d::a6]:50621 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50368 On 07/09/2013 10:54 PM, Frank Millman wrote: > > "Ian Kelly" wrote in message > news:CALwzidnf3Obe0eNf3xTHLj5a40K8HxVThVEipECQ8+34ZxyLxw@mail.gmail.com... >> On Tue, Jul 9, 2013 at 10:07 AM, Ethan Furman wrote: >>> You could also do it like this: >>> >>> def updating(self): >>> self.transaction_active = True >>> return self >> >> Yes, that would be simpler. I was all set to point out why this >> doesn't work, and then I noticed that the location of the >> "transaction_active" attribute is not consistent in the original code. >> The DbSession class places it on self, and then the example usage >> places it on the connection object (which I had based my version on). >> Since that seems to be a source of confusion, it demonstrates another >> reason why factoring this out is a good thing. > > 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? That concern is big enough. I've been bitten by that type of thing enough in my own code to want to avoid it where possible. Plus the `with db_session.updating() as conn:` saves keystrokes. ;) -- ~Ethan~