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


Groups > comp.lang.python > #89628

Re: Let exception fire or return None

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <antoon.pardon@rece.vub.ac.be>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'received:134': 0.05; '"""': 0.07; 'suppose': 0.07; 'indicates': 0.09; 'propagate': 0.09; 'subject:None': 0.09; 'way:': 0.09; 'python': 0.11; 'def': 0.12; 'question.': 0.14; 'big,': 0.16; 'fetch': 0.16; 'handled.': 0.16; 'subject:exception': 0.16; 'exception': 0.16; 'index': 0.16; 'do,': 0.16; 'do.': 0.18; 'file,': 0.19; "skip:' 30": 0.19; 'header:User-Agent:1': 0.23; 'error': 0.23; 'question': 0.24; 'least': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'raise': 0.29; 'skip:g 30': 0.30; 'specified': 0.30; 'code': 0.31; 'getting': 0.31; 'catching': 0.31; 'file:': 0.31; 'ok.': 0.31; 'project?': 0.31; 'such.': 0.31; 'yourself.': 0.31; 'this.': 0.32; 'handled': 0.32; 'problem': 0.35; "can't": 0.35; 'there': 0.35; 'returning': 0.36; 'similar': 0.36; 'should': 0.36; 'wrong': 0.37; 'too': 0.37; 'two': 0.37; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'catch': 0.60; 'most': 0.60; 'skip:o 30': 0.61; 'first': 0.61; 'more': 0.64; 'fire': 0.65; 'within': 0.65; 'pardon': 0.84; 'subject:Let': 0.91
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result AqYEAGgfQlWGuA9G/2dsb2JhbABch1bKFAKCGwEBAQEBAYUsAQEDASNVBgsLGgIFFgsCAgkDAgECAUUTCAKIHwizco51hFkBAQgCIIEhiheFDBaCUoFFBZwygSOGDIp+g1AjgWWCEYMyAQEB
Date Thu, 30 Apr 2015 14:30:34 +0200
From Antoon Pardon <antoon.pardon@rece.vub.ac.be>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Icedove/31.5.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Let exception fire or return None
References <87bni6awol.fsf@Equus.decebal.nl>
In-Reply-To <87bni6awol.fsf@Equus.decebal.nl>
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.128.1430397102.3680.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1430397102 news.xs4all.nl 2864 [2001:888:2000:d::a6]:58790
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89628

Show key headers only | View raw


Op 30-04-15 om 09:43 schreef Cecil Westerhof:

> I have a function to fetch a message from a file:
>     def get_indexed_message(message_filename, index):
>         """
>         Get index message from a file, where 0 gets the first message
>         """
>
>         return open(expanduser(message_filename), 'r').readlines()[index].rstrip()
>
> What is more the Python way: let the exception fire like this code
> when index is to big, or catching it and returning None?
>
> I suppose working zero based is OK.

I think this is the wrong question. The right question i: What is the
most logical/consistent thing to do, within your project?

There are at least two ways to look at this.

1) Getting too large an index is very similar to getting an index with an empty
line and can be treated as such. In that case you might prefer to return an
empty line.

2) Getting too large an index indicates a problem that can't be handled here. In
that case you should let the exception propagate to where it can be handled.

2a) You view the IndexError as a good diagnostic, nothing more to do.
2b) You view the IndexError as an error specific to the implementation
and want an exception independent of your implementation: catch the
IndexError and raise one you specified yourself.

-- 
Antoon Pardon

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


Thread

Let exception fire or return None Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 09:43 +0200
  Re: Let exception fire or return None Dave Angel <davea@davea.name> - 2015-04-30 04:18 -0400
    Re: Let exception fire or return None Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 11:13 +0200
  Re: Let exception fire or return None Peter Otten <__peter__@web.de> - 2015-04-30 11:30 +0200
    Re: Let exception fire or return None Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 13:26 +0200
      Re: Let exception fire or return None Peter Otten <__peter__@web.de> - 2015-04-30 14:28 +0200
        Re: Let exception fire or return None Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 16:53 +0200
      Re: Let exception fire or return None Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 14:22 +0200
  Re: Let exception fire or return None Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-04-30 14:05 +0200
  Re: Let exception fire or return None Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-04-30 14:30 +0200

csiph-web