Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'dynamically': 0.07; 'explicit': 0.07; 'stops': 0.07; '"if': 0.09; 'implements': 0.09; 'throws': 0.09; 'cc:addr:python-list': 0.11; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hint': 0.16; 'substitute': 0.16; 'exception': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'thu,': 0.19; 'otherwise,': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; "shouldn't": 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'am,': 0.29; 'raise': 0.29; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; '13,': 0.31; 'assert': 0.31; 'concern': 0.31; 'another': 0.32; 'cases': 0.33; 'checking': 0.33; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'two': 0.37; 'nov': 0.38; 'fact': 0.38; 'sure': 0.39; 'how': 0.40; 'more': 0.64; 'specialized': 0.65; 'here': 0.66; 'useful.': 0.68; 'incoming': 0.72; 'thing,': 0.91; '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=Rk1xMV9LDW5lOpmTLp0iyeuoAdO+tMe0FoQWui+U524=; b=hDdxMIyMfBu4Xh+MZibta2vHQ6AYObJtp6tFud5OiOvOBUojr+cZgwQLl9jORHSi1n hHD2lOGL6yF0vS7qxhT9Jewri2x/+uWYm+LK3gOfk78qU4RK+EEAyysw9HlAl40NpuUe 2e5ie6uYkpAE9XC4x/2wnpEo59qagtZ3Am3HebGcUbucB/LQbkbBPDh+VLq4HYjv/p9V WnbIYmAkdFvhRXm8gD39zc2v4wmFxkpylX9nK4mt0BZU4EsVo9sgDRKLXqxwmQ1iGJy+ nNdnSr+fjOEmucR9MVyzTvZXDtAOGlh3SyxZ5RnD3RIlTTx3+i6yoqMTXBczTVk5I+Vj DaUA== MIME-Version: 1.0 X-Received: by 10.107.130.218 with SMTP id m87mr51691294ioi.8.1415832203072; Wed, 12 Nov 2014 14:43:23 -0800 (PST) In-Reply-To: References: <96c138e4-ab95-4e5f-a0c8-e696b5a926bb@googlegroups.com> Date: Thu, 13 Nov 2014 09:43:22 +1100 Subject: Re: I love 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: 17 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415832205 news.xs4all.nl 2902 [2001:888:2000:d::a6]:40928 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80647 On Thu, Nov 13, 2014 at 7:53 AM, Anton wrote: > 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 > But the issues I can see here is that assert throws AssertError, while there is a specialized error for each of the case: 1) TypeError 2) ValueError. > Moreover for the 1) case it makes it impossible to dynamically substitute an object with another object that implements the same interface. The fact that there's a better exception type that's obviously more correct is a strong hint that you shouldn't use assert for these two cases. And your "Moreover" concern is a strong hint that you shouldn't be checking at all :) If you need to check this sort of thing, an explicit "if condition: raise SomeError('message')" construct is a lot more useful. Otherwise, there's no reason to have the line of code at all. ChrisA