Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Marko Rauhamaa Newsgroups: comp.lang.python Subject: Re: I love assert Date: Thu, 13 Nov 2014 01:12:44 +0200 Organization: A noiseless patient Spider Lines: 27 Message-ID: <87d28sovwz.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> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="ff5cf27ef3d5b31f034d3b72bdc27a41"; logging-data="15641"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Xnv45JBLEa7G6yuej2Eop" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:Z0M4L8ovGsYYp6g0K3e/kUNdENc= sha1:cBWD2lRbu9hDXOSJXkutrqqhv5Y= Xref: csiph.com comp.lang.python:80657 Ian Kelly : > On Wed, Nov 12, 2014 at 3:47 PM, Marko Rauhamaa wrote: >> 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. If assert weren't there, I would likely choose one of those two alternatives. But assert is there so there's no reason to avoid it. Marko