Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'happen?': 0.09; 'if,': 0.09; 'cc:addr:python-list': 0.11; '"hello': 0.16; 'detected': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'implies': 0.16; 'ought': 0.16; 'raised.': 0.16; 'reached,': 0.16; 'throw': 0.16; 'time.)': 0.16; 'exception': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'thu,': 0.19; 'cc:addr:python.org': 0.22; 'error': 0.23; 'certainly': 0.24; 'cc:2**0': 0.24; 'header :In-Reply-To:1': 0.27; 'am,': 0.29; 'raise': 0.29; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; '13,': 0.31; 'assert': 0.31; 'run': 0.32; 'sense': 0.34; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'false': 0.36; 'possible': 0.36; 'should': 0.36; 'nov': 0.38; "couldn't": 0.39; 'truly': 0.60; 'reached': 0.63; 'between': 0.67; 'optimized': 0.68; 'apart': 0.72; "it'd": 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=LiZUObim1mb5sj3Tzm0XLjLclaJCLwSKCYH+mZeQ+14=; b=Z/spwmuSNuJvgfDA9rPJ1m6jIwiPvj+UkBbn/CSkm++Suan/X3z9DVuxyeQ2sI2osA ENIYJbydO/RkqfLuufLvrKYYsFp90Ujh/Rojl2H17N2bKOzGO2kryC+4jtPALtebH7ua +uQHVWIG6OV6tFNCbw/yEt4LDQqUkZ8XXPvTsZ18jLjP/HOPj5TWN+BB+P9ctQ4Cbrjv ln1eq4LaUg/MPlWeaNvpKhtb5wRKG0xq1+fRHtx5EDVAW45LzVOT1kslDrFReDYNxPkt zOJShCJwCMT7AuvlQ2jfbnNzE71npXRP6TKnubQW/aVOeaAFlQrFq2wDN2PdBrMk2M7J d74Q== MIME-Version: 1.0 X-Received: by 10.50.128.35 with SMTP id nl3mr7607117igb.34.1415833127187; Wed, 12 Nov 2014 14:58:47 -0800 (PST) In-Reply-To: <87lhngox30.fsf@elektro.pacujo.net> References: <96c138e4-ab95-4e5f-a0c8-e696b5a926bb@googlegroups.com> <87a93wqeq3.fsf@elektro.pacujo.net> <87y4rgozgd.fsf@elektro.pacujo.net> <87lhngox30.fsf@elektro.pacujo.net> Date: Thu, 13 Nov 2014 09:58:47 +1100 Subject: Re: I love assert From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415833135 news.xs4all.nl 2877 [2001:888:2000:d::a6]:56524 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80652 On Thu, Nov 13, 2014 at 9:47 AM, Marko Rauhamaa wrote: > Apart from idiomatic style, there is no difference between > > # never reached > > assert False > > raise RuntimeError('Unreachable code reached') > > 1 / 0 > > print("Hello world") > > since, after all, that line is never reached! If it truly is never reached, there's no difference between any of the above and "pass". (With the possible exception of 1/0, which might plausibly be optimized to a constant, which would mean it'd throw an error at compile time.) But what if, one day, it is reached? What should happen? RuntimeError implies that there's an error that couldn't be detected until run time. AssertionError might not get raised. Printing "Hello world" to standard out will almost certainly be misinterpreted. ZeroDivisionError makes almost no sense for "shouldn't happen". Think about what your code ought to do *if it is reached*, because if you really don't care about that case, you won't have a line of code at all. ChrisA