Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79452
| Path | csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.005 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'comment,': 0.09; 'function,': 0.09; "person's": 0.09; 'preferable': 0.09; 'runtime': 0.09; 'useless': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '<>.': 0.14; "wouldn't": 0.14; '23,': 0.16; 'correctness': 0.16; 'fits': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "function's": 0.16; 'function?': 0.16; 'int)': 0.16; 'passes,': 0.16; 'redundant,': 0.16; 'suite.': 0.16; 'pushed': 0.16; 'language': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'code,': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; "shouldn't": 0.24; 'test.': 0.24; 'cc:2**0': 0.24; 'header:In- Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'rest': 0.29; 'chris': 0.29; 'am,': 0.29; 'waste': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'comments': 0.31; 'assert': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'there.': 0.32; 'checked': 0.32; 'another': 0.32; 'checking': 0.33; 'maybe': 0.34; "i'd": 0.34; 'agree': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'are,': 0.36; 'useful': 0.36; 'url:org': 0.36; 'should': 0.36; 'too': 0.37; 'level': 0.37; 'depends': 0.38; 'that,': 0.38; 'how': 0.40; 'most': 0.60; 'lower': 0.61; "you're": 0.61; 'our': 0.64; 'more': 0.64; 'believe': 0.68; 'anything.': 0.68; 'wish': 0.70; 'comment.': 0.84; 'guarantee.': 0.84; 'serious.': 0.84; 'technically': 0.84; 'to:none': 0.92; 'confidence': 0.95 |
| 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:cc :content-type; bh=9UTCe/A/IShXTKkVmA5zakPt13NdAXrsMdU84ZijTAw=; b=ub9HGB9U/5TGGc0oq5W182vADGZxRncSkJl4DpSwxZ47FrzpHIS+qD08+IeF7VD9/k Sq8gHChI0CYi/3cnR4gtv0g/i2vEBCiFeHrrm1tkIjenhFPQsPdVIqX6Zh1/aH/ElxGN NXGCxGc7WtmepUJpXz4ETAu3p+t9IOI3+6sx0S2PggVGL3FblJ8AfhM4hK0UE3QNCTp0 eaycX3DvRuKwHJueD5FSKwM2kKQEzN+yjJ8N/ejg5QAa9xwxZqQjB++L0neBpG9cMOsj PxebPCsNnDUWzPYRpwXzJvpK4EeQg7A7i4musv4UUzBNWWhQ3Xz8akA140Kg/x+JBs2f 2rEg== |
| MIME-Version | 1.0 |
| X-Received | by 10.107.136.68 with SMTP id k65mr27326613iod.43.1413993677866; Wed, 22 Oct 2014 09:01:17 -0700 (PDT) |
| In-Reply-To | <5447d20c$0$12997$c3e8da3$5496439d@news.astraweb.com> |
| References | <528871d5$0$29975$c3e8da3$5496439d@news.astraweb.com> <4d46b9b6-3ab9-45bc-afe9-55da4d0fe76c@googlegroups.com> <54470c17$0$12985$c3e8da3$5496439d@news.astraweb.com> <mailman.15053.1413942836.18130.python-list@python.org> <5447d20c$0$12997$c3e8da3$5496439d@news.astraweb.com> |
| Date | Thu, 23 Oct 2014 03:01:17 +1100 |
| Subject | Re: When to use assert |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15079.1413994041.18130.python-list@python.org> (permalink) |
| Lines | 49 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1413994041 news.xs4all.nl 2955 [2001:888:2000:d::a6]:43635 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:79452 |
Show key headers only | View raw
On Thu, Oct 23, 2014 at 2:49 AM, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > Chris Angelico wrote: > >> And at that point, the assertion is redundant, on par with: >> >> a = len(seq) >> assert isinstance(a, int) >> >> because you shouldn't have to assert what's part of a function's >> guarantee. > > That depends on how well you trust the function, how paranoid you are, and > whether you wish to include a checked comment, among other considerations. > I'd prefer to write the above than: > > a = len(seq) > # At this point, a is an int. > > because comments inside code that aren't checked are technically known > as "lies" <http://import-that.dreamwidth.org/956.html>. Ha ha only serious. > ... > Checking the post-condition of a built-in like len() is too paranoid for my > tastes... > > As I wrote: > > This is why assert can be divisive. Since we can vary in our > confidence about the correctness of code, one person's useful > assertion may be another person's useless runtime test. 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)? This is the job of a test suite. 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. Or is that insufficiently paranoid? ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: When to use assert Anton <anton.schattenfeld@gmail.com> - 2014-10-21 00:48 -0700
Re: When to use assert Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-22 12:44 +1100
Re: When to use assert Chris Angelico <rosuav@gmail.com> - 2014-10-22 12:53 +1100
Re: When to use assert Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-23 02:49 +1100
Re: When to use assert Chris Angelico <rosuav@gmail.com> - 2014-10-23 03:01 +1100
Re: When to use assert Dan Stromberg <drsalists@gmail.com> - 2014-10-22 09:18 -0700
Re: When to use assert Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-24 18:49 +1100
Re: When to use assert Chris Angelico <rosuav@gmail.com> - 2014-10-24 19:18 +1100
Re: When to use assert Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-26 20:39 +1100
Re: When to use assert Chris Angelico <rosuav@gmail.com> - 2014-10-26 20:51 +1100
csiph-web