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


Groups > comp.lang.python > #38172

Re: LBYL vs EAFP

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'algorithm': 0.03; 'exception.': 0.07; 'implements': 0.07; 'suppose': 0.07; 'finite': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'termination': 0.09; 'terry': 0.09; 'def': 0.10; 'do,': 0.15; 'library': 0.15; 'correctness': 0.16; 'eternal': 0.16; 'extensible': 0.16; 'forgiveness': 0.16; 'instance:': 0.16; 'partly': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subtraction': 0.16; 'wrote:': 0.17; 'documented': 0.17; 'instance': 0.17; 'tests.': 0.17; 'jan': 0.18; 'tests': 0.18; 'code,': 0.18; 'input': 0.18; '(or': 0.18; 'app': 0.19; 'code.': 0.20; 'all,': 0.21; 'driven': 0.22; "python's": 0.23; 'raise': 0.24; 'testing': 0.24; 'pass': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'used,': 0.27; 'header:X-Complaints-To:1': 0.28; 'bad.': 0.29; "d'aprano": 0.29; 'steven': 0.29; 'class': 0.29; 'classes': 0.30; "skip:' 10": 0.30; 'function': 0.30; 'code': 0.31; 'function.': 0.33; 'raising': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'times.': 0.33; 'wrong': 0.34; 'done': 0.34; 'third': 0.34; 'needed': 0.35; 'said,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:org': 0.36; 'but': 0.36; 'depends': 0.36; 'test': 0.36; 'option': 0.37; 'uses': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'nothing': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'between': 0.63; 'production': 0.63; 'safe': 0.63; 'different': 0.63; 'limit': 0.65; 'middle': 0.66; '"look': 0.84; 'blow': 0.84; 'moves': 0.84; 'presumption': 0.84; 'received:fios.verizon.net': 0.84; 'verifying': 0.84; 'responses': 0.93; 'silent': 0.95
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: LBYL vs EAFP
Date Tue, 05 Feb 2013 02:53:50 -0500
References <5110415c$0$29986$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0
In-Reply-To <5110415c$0$29986$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1357.1360050863.2939.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360050863 news.xs4all.nl 6922 [2001:888:2000:d::a6]:50386
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38172

Show key headers only | View raw


On 2/4/2013 6:16 PM, Steven D'Aprano wrote:
> The eternal conflict between "Look Before You Leap" and "Easier to Ask for
> Forgiveness than Permission" (LBYL vs EAFP) continues...

A somewhat different answer is that it depends on what you want the 
function to do, as documented and *tested*. And that partly depends on 
whether it is educational code for humans, production app code, or 
library code.

The test driven approach would be to write tests and then do what is 
needed to get them to pass. Doctests, unittests, and my private function 
test functions allow testing for raising a particular exception. 
AssertRaises() is used, perhaps increasingly, in stdlib tests. The 
absence of any tests for the response to 'bad' input suggests that the 
responses are 'undefined'.

That said, I admit that Python's extensible class system makes bad-input 
testing harder. I also think that anyone who uses non-builtin classes 
outside of their intended use area has to take responsibility.

For instance:

def f(n, a):
   if n < 0: raise ValueError('n cannot be negative')
   b = 0
   while n:
     b = process(a, b)
     n -= 1

looks like a safe LBYL function. But suppose n is an instance of a class 
that perversely implements subtraction as addition (or as doing 
nothing). Algorithm termination is based on the presumption that 
'decrementing' a 'positive value' moves it 'toward 0' and can only be 
done a finite number of times. Verifying that an input is a member of 
that abstract class is not trivial ;-).

 > A third option is not to check x at all, and hope that it will blow
 > up at some arbitrary place in the middle of my code rather than
 > silently do the wrong thing.

A silent infinite loop is bad. Infinite recursion stopped with the 
recursion limit check is less bad.

If tests pass with no check, then nothing need be done until one moves 
from correctness to resource use.

-- 
Terry Jan Reedy

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


Thread

LBYL vs EAFP Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-05 10:16 +1100
  Re: LBYL vs EAFP Chris Angelico <rosuav@gmail.com> - 2013-02-05 10:38 +1100
    Re: LBYL vs EAFP Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-05 03:52 +0000
      Re: LBYL vs EAFP Chris Angelico <rosuav@gmail.com> - 2013-02-05 16:19 +1100
  Re: LBYL vs EAFP Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-04 16:46 -0700
    Re: LBYL vs EAFP Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-05 04:52 +0000
      Re: LBYL vs EAFP Chris Angelico <rosuav@gmail.com> - 2013-02-05 16:20 +1100
        Re: LBYL vs EAFP Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-05 06:31 +0000
          Re: LBYL vs EAFP Pete Forman <petef4+usenet@gmail.com> - 2013-02-05 09:49 +0000
            Re: LBYL vs EAFP Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-05 23:04 +1100
              Re: LBYL vs EAFP Chris Angelico <rosuav@gmail.com> - 2013-02-05 23:25 +1100
      Re: LBYL vs EAFP Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-04 22:40 -0700
  Re: LBYL vs EAFP Dave Angel <davea@davea.name> - 2013-02-04 18:55 -0500
  Re: LBYL vs EAFP Chris Angelico <rosuav@gmail.com> - 2013-02-05 11:45 +1100
  Re: LBYL vs EAFP Ethan Furman <ethan@stoneleaf.us> - 2013-02-04 16:26 -0800
  Re: LBYL vs EAFP Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-05 01:00 +0000
  Re: LBYL vs EAFP Terry Reedy <tjreedy@udel.edu> - 2013-02-05 02:53 -0500

csiph-web