Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50368

Re: Default scope of variables

Date 2013-07-10 08:29 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Default scope of variables
References <51d4eb9c$0$29999$c3e8da3$5496439d@news.astraweb.com><mailman.4200.1372910878.3114.python-list@python.org><51d508ed$0$6512$c3e8da3$5496439d@news.astraweb.com><mailman.4208.1372916885.3114.python-list@python.org><51d5a504$0$29999$c3e8da3$5496439d@news.astraweb.com><CAPTjJmpgmABYwn0xB4T-9VbYMP=8JfpRcy8TPSerzyGvzPbMmA@mail.gmail.com><alpine.DEB.2.02.1307070759350.18702@gilgamesh><mailman.4357.1373204606.3114.python-list@python.org><krg4k4$tea$1@dont-email.me><mailman.4423.1373346452.3114.python-list@python.org><krg91s$e6j$1@dont-email.me><CAPTjJmqkmFd4-Jpugr-vubuB6riBV6K_Mwnxc_U3CVaBr_Wgbg@mail.gmail.com><krgefq$gji$1@ger.gmane.org><CALwzid=FzgjPebifx1stDBkh8iwLtWggwwPTPhZ1ykYg+05wEg@mail.gmail.com><krgi5f$n2v$1@ger.gmane.org> <51DC355E.20606@stoneleaf.us> <CALwzidnf3Obe0eNf3xTHLj5a40K8HxVThVEipECQ8+34ZxyLxw@mail.gmail.com> <krisu6$9oq$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.4529.1373471632.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 07/09/2013 10:54 PM, Frank Millman wrote:
>
> "Ian Kelly" <ian.g.kelly@gmail.com> wrote in message
> news:CALwzidnf3Obe0eNf3xTHLj5a40K8HxVThVEipECQ8+34ZxyLxw@mail.gmail.com...
>> On Tue, Jul 9, 2013 at 10:07 AM, Ethan Furman <ethan@stoneleaf.us> 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~

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 03:27 +0000
  Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-04 14:07 +1000
    Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 05:32 +0000
      Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-04 15:47 +1000
        Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 16:38 +0000
          Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-05 02:58 +1000
          Re: Default scope of variables Wayne Werner <wayne@waynewerner.com> - 2013-07-07 08:13 -0500
          Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-07 23:43 +1000
            Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-07 16:03 +0000
              Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-08 10:46 +1000
            Re: Default scope of variables alex23 <wuwei23@gmail.com> - 2013-07-09 14:52 +1000
              Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-09 15:07 +1000
                Re: Default scope of variables alex23 <wuwei23@gmail.com> - 2013-07-09 16:08 +1000
                Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-09 16:13 +1000
                Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-09 09:35 +0200
                Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-09 17:45 +1000
                Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-09 01:56 -0600
                Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-09 10:22 +0200
                Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-09 10:38 +0200
                Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-09 09:07 -0700
                Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-09 10:44 -0600
                Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-09 10:23 -0700
                Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-09 12:41 -0600
                Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-09 12:19 -0700
                Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-10 07:54 +0200
                Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-10 09:42 -0600
                Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-10 08:29 -0700
                Re: Default scope of variables "Frank Millman" <frank@chagford.com> - 2013-07-11 07:52 +0200
          Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-07 09:52 -0700
          Re: Default scope of variables Ethan Furman <ethan@stoneleaf.us> - 2013-07-07 11:59 -0700
          Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-08 10:48 +1000
            Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-08 02:23 +0000
              Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-08 13:11 +1000
                Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-08 05:00 +0000
                Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-08 15:14 +1000
      Re: Default scope of variables Peter Otten <__peter__@web.de> - 2013-07-04 08:48 +0200
      Re: Default scope of variables Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-04 01:12 -0600
      Re: Default scope of variables Dave Angel <davea@davea.name> - 2013-07-04 03:06 -0400
        Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 15:52 +0000
        Re: Default scope of variables Rotwang <sg552@hotmail.co.uk> - 2013-07-04 17:54 +0100
          Re: Default scope of variables Peter Otten <__peter__@web.de> - 2013-07-04 20:36 +0200
          Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-05 01:04 +0100
          Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-05 01:24 +0000
            Re: Default scope of variables Dave Angel <davea@davea.name> - 2013-07-04 22:03 -0400
            Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-05 03:29 +0100
            Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-05 03:27 +0100
            Re: Default scope of variables Rotwang <sg552@hotmail.co.uk> - 2013-07-05 07:28 +0100
        Re: Default scope of variables Neil Cerutti <neilc@norwich.edu> - 2013-07-05 13:24 +0000
          Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-05 23:43 +1000
            Re: Default scope of variables Neil Cerutti <neilc@norwich.edu> - 2013-07-05 15:36 +0000
          Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-07 16:08 +0000
            Re: Default scope of variables Neil Cerutti <neilc@norwich.edu> - 2013-07-08 11:54 +0000
              Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-08 14:14 +0100
      Re: Default scope of variables Lele Gaifax <lele@metapensiero.it> - 2013-07-04 14:43 +0200
      Re: Default scope of variables Wayne Werner <wayne@waynewerner.com> - 2013-07-04 10:45 -0500
  Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-04 05:30 +0100
    Re: Default scope of variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-04 05:45 +0000
  Re: Default scope of variables Chris Angelico <rosuav@gmail.com> - 2013-07-04 14:36 +1000
  Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-04 06:09 +0100
  Re: Default scope of variables Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-08 17:58 +0100

csiph-web