Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.101 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.80; '*S*': 0.00; 'detect': 0.07; 'expectation': 0.09; 'reached.': 0.09; 'convey': 0.16; 'executed,': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'programming': 0.22; 'error': 0.23; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'assert': 0.31; 'comment': 0.34; "i'd": 0.34; 'something': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'two': 0.37; 'easily': 0.37; 'being': 0.38; 'nov': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'anything': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'ian': 0.60; 'most': 0.60; 'effective': 0.61; 'reached': 0.63; 'between': 0.67; 'optimized': 0.68; 'apart': 0.72; 'honest': 0.78 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=empcu6b4TtckYT5XnLwbb56ODB2YV0USXd9jRHGiHs8=; b=QOKmrzAlUll28JDhghK2YPoQt0yRKqiENVsK9iofw9dnuh4UWBUqBF6qeje8E4lK2G xj/dqpEOOaOA8JNJ0Vd0170wSsiU6OO3nVOL37LcRk1SYKf2jkYV+iSqU+nkSNjKXzPK O4aF0DCJL6IdsF05BSuvHGyQBKqSucAKNgxhUHGIS3idx3LltgFcLLo41xwYNoLXimcE f3N1AWNbAoQtkw6efH7gd0+6jSj1aasF3127BbBY8vaVxGJOXokgSikYhiEgw+s338Iz AgTuh7gjUTHyYuEUCc+3V72yEOFv8sdTr3joBp53SuJDJnOIx/q0bsd/N/+akLodoIk2 buNg== X-Received: by 10.66.163.71 with SMTP id yg7mr43141039pab.110.1415833586630; Wed, 12 Nov 2014 15:06:26 -0800 (PST) MIME-Version: 1.0 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> From: Ian Kelly Date: Wed, 12 Nov 2014 16:05:46 -0700 Subject: Re: I love assert To: Python 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415833594 news.xs4all.nl 2894 [2001:888:2000:d::a6]:35702 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:80655 On Wed, Nov 12, 2014 at 3:47 PM, Marko Rauhamaa wrote: > Ian Kelly : > >> Although to be honest I'd rather use something like "raise >> RuntimeError('Unreachable code reached')" than "assert False" here. If >> the expectation is that the code will never be executed, then there's >> no reason to ever optimize it out. > > Asserts have nothing to do with them being optimized out. Asserts are > communication. > > Apart from idiomatic style, there is no difference between > > # never reached > > assert False > > raise RuntimeError('Unreachable code reached') If the purpose is communication, then the comment is most effective, as it can easily convey anything you want. If the purpose is to detect programming errors, then the RuntimeError is most effective, as it will always raise an error in the event it is reached. assert False is a strange hybrid of the two that is less effective at both purposes.