Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.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.041 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'error:': 0.07; 'modified': 0.07; 'indicates': 0.09; 'then?': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'wed,': 0.18; '>>>': 0.22; 'example': 0.22; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'assert': 0.31; 'run': 0.32; 'actual': 0.34; 'there,': 0.34; 'received:google.com': 0.35; 'false': 0.36; 'should': 0.36; 'being': 0.38; 'e.g.': 0.38; 'nov': 0.38; 'handle': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; '12,': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'remove': 0.60; 'new': 0.61; 'optimized': 0.68; 'ethan': 0.84; 'furman': 0.84; 'warning.': 0.84; 'imagine': 0.93 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=/++ST42HJP/pz48Uuyk/Zhl0xqC7OIcccbumeJiGbuU=; b=0TvjxasXxY7iIFt7h9L2folxe3oVPFt+IwjZRpmb/J5f74C6IGTmwUiPtPhFveVKmv yM6D99sWGI0C2Sbo/3GLPlXSKmY/3P5P3o3v6bNeG778zdrfdoI62Ih0kf3c9k91bWn7 3DI8S4Ys33WnKN0ZJCIlCt4fFtN9YW8UJ9XO+A0yoJwFFft57ngaAFYP40ZPSyGbQp7U N+eTTjCyI1biSL/upaqa7DAnEJ0Sca9HK6JC6N0atpQZh+eOXHoLJTnhtMyfhwQSARD7 SwCSUitlUM415MQk6by8ptTJsnl1RzeSGhzoiMps4QtARLwfz6xsljrFNntgNELTU7e5 XVWw== X-Received: by 10.70.118.72 with SMTP id kk8mr22421970pdb.147.1415829896455; Wed, 12 Nov 2014 14:04:56 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <87y4rgozgd.fsf@elektro.pacujo.net> References: <96c138e4-ab95-4e5f-a0c8-e696b5a926bb@googlegroups.com> <87a93wqeq3.fsf@elektro.pacujo.net> <87y4rgozgd.fsf@elektro.pacujo.net> From: Ian Kelly Date: Wed, 12 Nov 2014 15:04:16 -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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415829905 news.xs4all.nl 2947 [2001:888:2000:d::a6]:44383 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80640 On Wed, Nov 12, 2014 at 2:56 PM, Marko Rauhamaa wrote: > Ethan Furman : > >> On 11/12/2014 01:41 PM, Marko Rauhamaa wrote: >>> >>> Or I might indicate the exhaustion of possibilities: >>> >>> if status == OK: >>> ... >>> elif status == ERROR: >>> ... >>> else: >>> assert status == WARNING >>> ... >> >> And here's a nice example of what one should NOT do. Imagine that a >> new status, CONFUSED is added, the above code is not modified to >> handle it, and for whatever reason the program is being run optimized >> -- the assert is not there, and CONFUSED is treated the same as >> WARNING. > > How would it be better if you removed the assert then? You don't need to remove it. Just reorganize it to make sure it indicates actual exhaustion of possibilities. E.g. using the "assert False" pattern from your post: if status == OK: ... elif status == ERROR: ... elif status == WARNING: ... else: assert False