Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.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.116 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.77; '*S*': 0.00; 'bug.': 0.09; 'typed': 0.09; 'attributes,': 0.16; 'concern,': 0.16; 'objection': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'commit': 0.19; 'code,': 0.22; 'looks': 0.24; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '(unless': 0.31; 'concern.': 0.31; 'once,': 0.31; 'this.': 0.32; 'checked': 0.32; 'another': 0.32; 'quite': 0.32; 'actual': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'transaction': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'possible': 0.36; 'error.': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'here': 0.66; 'answer.': 0.68; 'frank': 0.68; 'jul': 0.74; 'dry': 0.84; 'mistake': 0.91; 'mistakenly': 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:from:date:message-id:subject:to :content-type; bh=f6PieZ6dEAF61NDtXOto2jnxkgjftUwDkWmXkQf3Yr0=; b=k8MYsYj9SgzeS7qZ3OCrBmS0MushcjB/34mQ/9flMQld3lFmTlFPx8tcJScC7m88el jqMp+/USsveoiHg8Eyfq9SlL6hIrAgg4OVzHfS1RgbFF6rxzbGD2H9gxRouMvsQf7bfo r6Qcwj1iQyIDrNocLVlrSWqOYy1tp/pzUfIWKewPMzl175mpukLz4Hd3/DoHeXZ3JPoA SGOn1hofC2uzkLMptAArFWf0f8ziBYRk7+9ebGT7UAWV/Pd+kg7U80VfyH/QpZhTumto mqIqv8/FPGsUoQjuwkCSM/gFRcXCxMRxXq1bDRayuHAckmFtLOeBbHd8m8Bj6IjKRGXm jhWg== X-Received: by 10.68.29.2 with SMTP id f2mr32199760pbh.184.1373470969347; Wed, 10 Jul 2013 08:42:49 -0700 (PDT) MIME-Version: 1.0 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> <51DC355E.20606@stoneleaf.us> From: Ian Kelly Date: Wed, 10 Jul 2013 09:42:08 -0600 Subject: Re: Default scope of variables To: Python 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373470972 news.xs4all.nl 15942 [2001:888:2000:d::a6]:40821 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50364 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.