Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!news.muarf.org!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed3a.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'source.': 0.07; 'tests.': 0.07; 'string': 0.09; '"if': 0.09; '22,': 0.09; 'abstraction': 0.09; 'if,': 0.09; 'preferable': 0.09; 'tests,': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; "wouldn't": 0.14; '23,': 0.16; 'debugging,': 0.16; 'fine.': 0.16; 'fire,': 0.16; 'fits': 0.16; 'function?': 0.16; 'passes,': 0.16; 'suite.': 0.16; 'typo': 0.16; 'violation.': 0.16; 'pushed': 0.16; 'language': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'thu,': 0.19; 'code,': 0.22; 'cc:addr:python.org': 0.22; 'tend': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'developers': 0.25; 'changes,': 0.26; 'header:In- Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'rest': 0.29; 'chris': 0.29; 'am,': 0.29; 'raise': 0.29; 'waste': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'too.': 0.31; 'assert': 0.31; "d'aprano": 0.31; 'lot.': 0.31; 'steven': 0.31; 'suites': 0.31; 'there.': 0.32; 'probably': 0.32; 'another': 0.32; 'bugs': 0.33; 'checking': 0.33; 'maybe': 0.34; 'could': 0.34; "can't": 0.35; 'agree': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'described': 0.36; 'much.': 0.36; "i'll": 0.36; 'should': 0.36; 'example,': 0.37; 'level': 0.37; 'that,': 0.38; 'even': 0.60; 'company': 0.60; 'above,': 0.60; 'most': 0.60; 'lower': 0.61; "you're": 0.61; 'more': 0.64; 'love': 0.65; 'to:addr:gmail.com': 0.65; 'believe': 0.68; 'anything.': 0.68; 'reliance': 0.72; 'comment.': 0.84 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:to :cc:content-type; bh=XfwW/oQ0BWUWKbot/DLqMyx1/39N/apOjwtxGdpEM7k=; b=XCnw7uuosIlyd7v7m4OKXQJ61x9R5fAimakysYBxAWFbMNV3vMYlY2OZSy06CwBQ1B sCwFq8GpfDV+xmhr6wYTxbM8YXQmNwhopntf+RVfm32U3yDHVmBEf68Fo+krWNuyNOXl oNthjjXPiLYpcc+mmUk2T3T36Y78+XCiUj4a2kMPUzpt2CndBGKZ+JR47UVG9knbdU0r OJ6R5TI9S49A55CLvn6Z4RUfot5mUF6vLpwxktXnj6UuM1bBOMAlURIGeyTLTgybtVhZ b1egOCF+Tcw5Lbi++5U5h5UFzaWa/2WdbXbBGddzoIBreXEEtn6F6TgAYBqhqjKdC9T/ cNdQ== MIME-Version: 1.0 X-Received: by 10.112.147.225 with SMTP id tn1mr42758641lbb.37.1413994704775; Wed, 22 Oct 2014 09:18:24 -0700 (PDT) In-Reply-To: References: <528871d5$0$29975$c3e8da3$5496439d@news.astraweb.com> <4d46b9b6-3ab9-45bc-afe9-55da4d0fe76c@googlegroups.com> <54470c17$0$12985$c3e8da3$5496439d@news.astraweb.com> <5447d20c$0$12997$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 22 Oct 2014 09:18:24 -0700 Subject: Re: When to use assert From: Dan Stromberg To: Chris Angelico Content-Type: text/plain; charset=UTF-8 Cc: "python-list@python.org" 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413994712 news.xs4all.nl 2838 [2001:888:2000:d::a6]:49939 X-Complaints-To: abuse@xs4all.nl X-Received-Body-CRC: 2446707719 X-Received-Bytes: 6528 Xref: csiph.com comp.lang.python:79454 On Wed, Oct 22, 2014 at 9:01 AM, Chris Angelico wrote: > On Thu, Oct 23, 2014 at 2:49 AM, Steven D'Aprano > wrote: >> Chris Angelico wrote: > I agree that the assert is preferable to the comment. But maybe my > level of paranoia is just lower than most people's, as I wouldn't > bother checking the post-conditions of pretty much anything. Do you > put these assertions every time you call the function? Every time > you're depending on its return value? At what point should you stop > writing Python code and start using a language with a lot more > boilerplate and checking (I believe Haskell fits that, though I'm not > overly familiar with the language)? I like to use assertions and "if cond: raise ValueError('foo')" a lot. I think Eiffel may be the poster-child for a language with pre-conditions, post-conditions and assertions. I think you're in good company - a lot of developers don't use assertions much. I like assertions, because they tend to stop bugs pretty quickly. If you have 3 functions, one calling another calling another, assertions in each can keep you from having to backtrack among them when debugging, instead going directly to the problem's source. > This is the job of a test suite. Test suites are great, and I can't really question your reliance on them. I love having lots of automated tests. But for the reason I described above, I still like having lots of assertions. > You don't pepper your code with > assertions to the effect that "I just pushed something onto my queue, > it should now have this item in it"; you create a test case for it, > and verify your function there. In the rest of the code, you trust > that your test suite passes, and don't waste time with assertions. I wouldn't test that a value was added to a queue immediately after adding it. That's excessive, and may even require an abstraction violation. But if, for example, I have a string with 3 known-good values, I'll if/elif/elif/else, and make the else raise an AssertionError. The assertion should never fire, but if the code changes, it could, and if there's a typo somewhere, it could then too. > Or is that insufficiently paranoid? With good tests, you're probably fine.