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: Wed, 12 Nov 2014 23:41:08 +0200 Organization: A noiseless patient Spider Lines: 43 Message-ID: <87a93wqeq3.fsf@elektro.pacujo.net> References: <96c138e4-ab95-4e5f-a0c8-e696b5a926bb@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="ff5cf27ef3d5b31f034d3b72bdc27a41"; logging-data="29715"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/DzO2VrxRfhfUVG2fW0c3B" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:jt8+l0i3ESyJlyNIAzNi+7Y1q6k= sha1:tocGL/GWOQ8GoIxi9pJCmzae35w= Xref: csiph.com comp.lang.python:80632 Anton : > I am not sure how and when you use assert, but something that stops me > from using assert is that in many cases I would use them to > 1) Check type of an incoming argument/returned value > 2) Check value of an incoming argument/returned value You use asserts to boldly state that the asserted condition never fails. If it should fail anyway, the world could come tumbling down and you would hang your head in shame. Asserts help the reader of the code understand why some possibilities are not considered by the code. They are not considered because the writer of the code asserts they are not really possible. Asserts help not only lay the blame to the writer of the code but might also make it easier to troubleshoot failures. I use asserts sparingly, just like comments (which asserts are). I use them to communicate nonobvious truths to the future maintainer of the code. For example, instead of # never reached I will write assert False Or I might indicate the exhaustion of possibilities: if status == OK: ... elif status == ERROR: ... else: assert status == WARNING ... Marko