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


Groups > comp.lang.python > #89626

Re: Let exception fire or return None

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.python
Subject Re: Let exception fire or return None
Date Thu, 30 Apr 2015 14:05:22 +0200
Organization PointedEars Software (PES)
Lines 41
Message-ID <6844153.DioSdhvfDQ@PointedEars.de> (permalink)
References <87bni6awol.fsf@Equus.decebal.nl>
Reply-To Thomas 'PointedEars' Lahn <usenet@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1430395672 24917 eJwFwQkBACAIBLBK8h0YBxD6R3AzAaFdYVBbWwZ1xKADSsejVkdjzVH+hJsHJ3EzU3Jq5QMPVRE5 (30 Apr 2015 12:07:52 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Thu, 30 Apr 2015 12:07:52 +0000 (UTC)
User-Agent KNode/4.14.2
X-User-ID eJwFwQkBACAIBLBK8h0QR0D6R3AzAaFdYVBb29JLWiE9RShy1kWGC78T3t7p+zpBeycaPWpDd2SYc94x+1g1Fc0=
Cancel-Lock sha1:EpKdk6Mhe0NhZjsrNGYZYyAH6mw=
X-NNTP-Posting-Host eJwNyMERACAIA7CVKNJyjKNy7D+CfvIIl6CbISo4nG26aBiO96wt85xP/Qo5/GQPjZ7JrEI9CgcP0Q==
Xref csiph.com comp.lang.python:89626

Show key headers only | View raw


Cecil Westerhof wrote:

> 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?

It is not only the Python way, it is the *correct* way to throw/_raise_ an 
exception if the language allows it.  Think of what “exception” means: there 
is an *exceptional* (not normal) circumstance that causes execution of the 
function/method to stop.

Return values and status values to indicate such errors were used before 
exceptions, and are still used in languages that do not have exceptions.  
But return values only work if you can tell them apart from success values.  
You have only one error condition now.  If you have a different error 
condition in the future, you need a different return value for that to tell 
it apart from the first error condition.  

There comes a point where you are out of new return values for new error 
conditions such that you cannot tell them apart from success values anymore.  
And functions setting status values to refine return values are not 
reentrant.  But you can always throw a different type of exception or the 
same type of exception with a different message.

> I suppose working zero based is OK.

Yes, it is the common approach.

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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