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


Groups > comp.lang.python > #34378

Re: Confused compare function :)

Date 2012-12-06 08:55 -0300
From peter <pjmakey2@gmail.com>
Subject Re: Confused compare function :)
References (1 earlier) <mailman.538.1354753193.29569.python-list@python.org> <k9p32p$nek$1@dont-email.me> <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <mailman.549.1354783761.29569.python-list@python.org> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.554.1354795380.29569.python-list@python.org> (permalink)

Show all headers | View raw


On 12/06/2012 08:47 AM, Steven D'Aprano wrote:
> On Thu, 06 Dec 2012 09:49:26 +0100, Bruno Dupuis wrote:
>
>> The point is Exceptions are made for error handling, not for normal
>> workflow.
> That's certainly not the case in Python. Using exceptions for flow
> control is a standard part of the language.
>
> IndexError and StopIteration are used to detect the end of lists or
> iterators in for loops.
>
> GeneratorExit is used to request that generators exit.
>
> SysExit is used to exit the interpreter.
>
>
> There is nothing wrong with using exceptions for flow control in
> moderation.
>
>
>> I hate when i read that for example:
>>
>>      try:
>>          do_stuff(mydict[k])
>>      except KeyError:
>>          pass
>>
>> (loads of them in many libraries and frameworks) instead of:
>>
>>      if k in mydict:
>>          do_stuff(mydict[k])
>>
>> Note that the performances are better with the latter.
> Not so. Which one is faster will depend on how often you expect to fail.
> If the keys are nearly always present, then:
>
> try:
>      do_stuff(mydict[k])
> except KeyError:
>      pass
>
> will be faster. Setting up a try block is very fast, about as fast as
> "pass", and faster than "if k in mydict".
>
> But if the key is often missing, then catching the exception will be
> slow, and the "if k in mydict" version may be faster. It depends on how
> often the key is missing.
>
>
> [...]
>> It depends also on the context, I'd be more 'permissive' a short script
>> than into a large program, framework, or lib, for the very reason it's
>> easy to know all code interactions.
>>
>> In my coder life, i spent more time debugging silently swallowed
>> exceptions than logging abnormal behaviours.
>
> That's fine. I agree with you about not silently swallowing errors. Where
> I disagree is that you said "never ever", which is an exaggeration.
> Remember that exceptions are not always errors.
>
> Problem: take a list of strings, and add up the ones which are integers,
> ignoring everything else.
>
> Solution:
>
> total = 0
> for s in list_of_strings:
>      try:
>          total += int(s)
>      except ValueError:
>          pass  # Not a number, ignore it.
>
>
> Why would you want to log that? It's not an error, it is working as
> designed. I hate software that logs every little thing that happens, so
> that you cannot tell what's important and what isn't.
>
>
>
Is perfectly right to use try catch for a flow control.
Just think in something more complex like this.

    try:
             self._conn = MySQLdb.connect(host=host,
                 user=user,
                 passwd=passwd,
                 db=db)
     except:
             logging.info("Error de conexion con la base de datos")
             inform(subject = 'Db down on app %s' % app, body=sbody)

Or maybe something like this.

try:
             cursor.execute(sqli, data)
             self._conn.commit()
except:
             try:
                 self._conn.rollback()
                 cursor.execute(sqli, data)
                 self._conn.commit()
             except Exception, e:
                 pass
                 # print e
                 # logging.info('ERROR en la insercion %s' % e)

This is pretty dumb, but is a valid example, on what you can do with try 
catch

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


Thread

Re: Confused compare function :) Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-06 01:19 +0100
  Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 00:42 +0000
    Re: Confused compare function :) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-12-06 13:41 -0500
    Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 19:55 +0100
  Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 03:22 +0000
    Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 04:32 +0000
      Re: Confused compare function :) Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-06 09:49 +0100
        Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 11:47 +0000
          Re: Confused compare function :) peter <pjmakey2@gmail.com> - 2012-12-06 08:55 -0300
            Re: Confused compare function :) Hans Mulder <hansmu@xs4all.nl> - 2012-12-06 14:32 +0100
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 00:47 +1100
          Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-06 23:14 +1100
            Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-07 22:16 +0000
              Re: Confused compare function :) Terry Reedy <tjreedy@udel.edu> - 2012-12-08 02:01 -0500
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-08 18:17 +1100
              Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-08 17:50 +0000
            Re: Confused compare function :) Ramchandra Apte <maniandram01@gmail.com> - 2012-12-08 19:07 -0800
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-09 14:22 +1100
                Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-09 07:39 +0000
            Re: Confused compare function :) Ramchandra Apte <maniandram01@gmail.com> - 2012-12-08 19:07 -0800
          Re: Confused compare function :) Neil Cerutti <neilc@norwich.edu> - 2012-12-06 13:51 +0000
            Re: Confused compare function :) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-07 02:55 +0000
              Re: Confused compare function :) Neil Cerutti <neilc@norwich.edu> - 2012-12-07 16:40 +0000
        Re: Confused compare function :) Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-12-06 14:33 +0100
          Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 00:58 +1100
            Re: Confused compare function :) Hans Mulder <hansmu@xs4all.nl> - 2012-12-06 15:21 +0100
              Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-07 01:28 +1100
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 15:22 +0100
          Re: Confused compare function :) Dave Angel <d@davea.name> - 2012-12-06 09:40 -0500
          Re: Confused compare function :) peter <pjmakey2@gmail.com> - 2012-12-06 11:46 -0300
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 17:16 +0100
          Re: Confused compare function :) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-12-06 16:52 +0000
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 18:08 +0100
          Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-06 17:10 +0000
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 18:31 +0100
          Re: Confused compare function :) MRAB <python@mrabarnett.plus.com> - 2012-12-06 17:52 +0000
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-06 19:25 +0100
          Re: Confused compare function :) Anatoli Hristov <tolidtm@gmail.com> - 2012-12-07 14:36 +0100
        Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 19:24 +0000
      Re: Confused compare function :) Chris Angelico <rosuav@gmail.com> - 2012-12-06 20:34 +1100
      Re: Confused compare function :) Rotwang <sg552@hotmail.co.uk> - 2012-12-06 19:25 +0000

csiph-web