Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!border1.nntp.ams2.giganews.com!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'example:': 0.03; 'exception': 0.03; 'debugging': 0.05; 'that?': 0.05; 'except:': 0.07; 'exit': 0.07; 'interpreter.': 0.07; 'present,': 0.07; 'problem:': 0.07; 'try:': 0.07; 'valueerror:': 0.07; '"if': 0.09; "%s'": 0.09; 'app,': 0.09; 'exception,': 0.09; 'exit.': 0.09; 'generators': 0.09; 'handling,': 0.09; 'ignoring': 0.09; 'received:mail-vc0-f174.google.com': 0.09; 'ignore': 0.13; 'dec': 0.15; 'data)': 0.16; 'ever",': 0.16; 'fine.': 0.16; 'framework,': 0.16; 'frameworks)': 0.16; 'int(s)': 0.16; 'integers,': 0.16; "isn't.": 0.16; 'iterators': 0.16; 'missing,': 0.16; 'moderation.': 0.16; 'slow,': 0.16; 'subject:compare': 0.16; 'swallowed': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'detect': 0.17; 'thu,': 0.17; 'app': 0.19; 'exceptions': 0.22; 'keyerror:': 0.22; 'keys': 0.22; 'libraries': 0.22; "i'd": 0.22; 'nearly': 0.23; 'so.': 0.24; 'script': 0.24; 'pass': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'setting': 0.26; 'am,': 0.27; 'language.': 0.27; 'logging': 0.27; 'errors.': 0.27; '+0100,': 0.29; 'catching': 0.29; 'context,': 0.29; "d'aprano": 0.29; 'received:209.85.220.174': 0.29; 'steven': 0.29; 'strings,': 0.29; 'this.': 0.29; 'maybe': 0.29; 'error': 0.30; 'expect': 0.31; 'lists': 0.31; 'code': 0.31; 'point': 0.31; 'print': 0.32; 'to:addr:python-list': 0.33; 'version': 0.34; 'agree': 0.34; 'program,': 0.34; 'received:google.com': 0.34; 'wrong': 0.34; 'list': 0.35; 'faster': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'add': 0.36; 'except': 0.36; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'depends': 0.36; 'flow': 0.36; 'why': 0.37; 'ones': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'short': 0.39; 'little': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'think': 0.40; 'easy': 0.60; 'life,': 0.62; 'more': 0.63; 'total': 0.65; 'else.': 0.65; 'of:': 0.65; 'received:200': 0.71; 'abnormal': 0.84; 'fast,': 0.84; 'faster.': 0.91; 'hate': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=UZcuzL9b4nT6G6H2R9rLne84gc7MozYkiGfm25PQqZQ=; b=R9ZPAkgd3ISPuvk2aHlaYlfBHBENh1onnJuWXEdhuLCdJ6+V6iO2tD/VsrJO/YQWJW PLgIg1odhBxSuAx4sOtdW8FFgA1IcgEa9Gj8VsFBUMT7EGv2pTUo9HYFX1Y67AkHeHCC c7oYPJ6VMoKqypLLbInN/IQcKNLRRCxbHJEnRTMXPIK3kUDllOA4/Yfd129wkX3976Va Bbsz3XmhDXnkOZV/ovNQIlfRcLHwi1RHn58EecZ3fXyIx3IAqvGA0+7Sv3JPGTCIrLZs 4lTKbNIcCWHuJBoj8V2ZSvQL+HDaL7QAwqwrGYO6GiAKZuO0KD/dk2HC6FNtBAL8qqjj F2Jw== Date: Thu, 06 Dec 2012 08:55:16 -0300 From: peter User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120824 Thunderbird/15.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Confused compare function :) References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 109 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354795380 news.xs4all.nl 6971 [2001:888:2000:d::a6]:53511 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34378 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