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; 'removes': 0.07; 'builtin': 0.09; 'expression:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'separating': 0.09; 'statements': 0.09; 'tests,': 0.09; 'python': 0.11; 'jan': 0.12; 'mostly': 0.14; 'pythonistas': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'stdlib.': 0.16; 'substitute': 0.16; 'variations': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'all,': 0.19; 'not,': 0.20; 'coding': 0.22; 'tests': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; "aren't": 0.24; 'removed.': 0.24; 'equivalent': 0.26; 'nearly': 0.26; 'header:X-Complaints-To:1': 0.27; 'header :In-Reply-To:1': 0.27; 'raise': 0.29; "doesn't": 0.30; 'statement': 0.30; 'code': 0.31; 'assert': 0.31; 'exceptions': 0.31; 'run': 0.32; 'message.': 0.35; 'basic': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'impression': 0.36; 'useful': 0.36; 'should': 0.36; 'unit': 0.37; 'depends': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'truly': 0.60; 'most': 0.60; 'more': 0.64; 'love': 0.65; 'specialized': 0.65; 'removal': 0.74; '100': 0.79; '"active': 0.84; 'action.': 0.84; 'bare': 0.84; 'received:fios.verizon.net': 0.84; 'usage.': 0.84; 'am.': 0.91; 'passive': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: I love assert Date: Tue, 11 Nov 2014 17:34:27 -0500 References: <96c138e4-ab95-4e5f-a0c8-e696b5a926bb@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-108-16-203-145.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <96c138e4-ab95-4e5f-a0c8-e696b5a926bb@googlegroups.com> 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415745600 news.xs4all.nl 2870 [2001:888:2000:d::a6]:41014 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!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:80572 On 11/11/2014 2:40 PM, Peter Cacioppi wrote: > I get the impression that most Pythonistas aren't as habituated with > assert statements as I am. Is that just a misimpression on my part? > If not, is there a good reason to assert less with Python than other > languages? We love 'assert' so much that we have 20-30 'assertXYZ' variations in unittest. The statement 'assert expression' is almost equivalent to if not expression: raise AssertionError('expression') We have perhaps 100 mostly more specialized exceptions builtin and in the stdlib. There is nearly always a better, more specific exception and error message. > As far as I can tell, Python supports assert perfectly well. When run > with the optimization flagging, the asserts are truly removed. Separating tests from main code removes test asserts from the main code. Removal by -0 relegates bare assert to specialized usage. > I think one needs to take care with some basic assert coding - it's > not a substitute for unit tests, it doesn't absolve you of normal > exception responsibilities, and, most of all, it should be used for > passive inspection and not action. But given these guidelines, I > still find it very useful as "active comments". How much usage depends considerably on the programmer. -- Terry Jan Reedy