Return-Path: 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; '22,': 0.09; 'logic': 0.09; 'method,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "function's": 0.16; 'int)': 0.16; 'redundant,': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'cc:addr:python.org': 0.22; 'integer': 0.24; "shouldn't": 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'assert': 0.31; 'assumes': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'acceptable': 0.36; 'pm,': 0.38; 'guarantee.': 0.84; 'to:none': 0.92 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=jyh7uI425A7OqiDcUwmbalDvl/Q9+Zf/5Miotj/2o04=; b=wex8PfVjF4uMdt+9Lyk+eCJnzwXkN8Ac6vtJq/YbsG2gR++uClQwGt7OUFDbfyuiOx 1vMxE9mBtG8i3qbLcvJW3YIQiMBs4Vh0TpzCdoCVTKoswudfeDZy/tWad8jTPuemD/aP dpkIbhcjhn5MQN1+2PMYJ9oqiYpUaLZbbHxU00fh7ssa+1+QLwLCmOUItCQz2SDLvZow CCUUsm6BQoCv65z4YNGdp4LewV29QgFQlQHRz4fwuPQIuu+rfKUu66cEl+6jZcc6uUzJ KGJw6ocPX71JnbPWXnlMQeG1lIJAVC8LMyJGqDzvQNQ6FhpNdEcmjEJUEBz+hEMUmi0A VGrw== MIME-Version: 1.0 X-Received: by 10.50.61.137 with SMTP id p9mr2124762igr.34.1413942826672; Tue, 21 Oct 2014 18:53:46 -0700 (PDT) In-Reply-To: <54470c17$0$12985$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> Date: Wed, 22 Oct 2014 12:53:46 +1100 Subject: Re: When to use assert From: Chris Angelico Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413942836 news.xs4all.nl 2914 [2001:888:2000:d::a6]:36579 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:79408 On Wed, Oct 22, 2014 at 12:44 PM, Steven D'Aprano wrote: > def do_something(instance_or_id): > instance = Model.get(instance_or_id) > assert isinstance(instance, Model) > # Code that assumes that instance is an object of type Model > > > That means that the logic for what is acceptable as a Model is all in one > place, namely the Model.get method, and callers don't need to care about > the pre-condition "argument is a Model or an integer ID", they only need to > care about the post-condition "result of get() is a Model". 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. ChrisA