Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'anyway.': 0.05; 'args': 0.07; 'debugging': 0.07; 'executed': 0.09; 'function,': 0.09; 'raises': 0.09; 'scripts,': 0.09; 'thrown': 0.09; 'worse': 0.09; 'python': 0.11; 'def': 0.12; 'bug': 0.12; '(there': 0.16; 'added.': 0.16; 'bug,': 0.16; 'clear.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'manifest': 0.16; 'mixture': 0.16; 'rerunning': 0.16; 'sorts': 0.16; 'types,': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'bit': 0.19; '(but': 0.19; 'stack': 0.19; 'code,': 0.22; 'programming': 0.22; 'aug': 0.22; "aren't": 0.24; 'case.': 0.24; 'versions': 0.24; 'sort': 0.25; 'source': 0.25; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'testing': 0.29; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'errors': 0.30; 'mix': 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'code': 0.31; 'usually': 0.31; '(usually': 0.31; 'checking.': 0.31; 'exceptions': 0.31; 'fighting': 0.31; 'safely': 0.31; 'this.': 0.32; 'could': 0.34; 'problem': 0.35; "can't": 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'in.': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'half': 0.37; 'wrong': 0.37; 'too': 0.37; 'two': 0.37; 'problems': 0.38; 'checks': 0.38; 'to:addr:python- list': 0.38; 'that,': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'simple,': 0.60; 'most': 0.60; "you're": 0.61; 'save': 0.62; 'times': 0.62; "you'll": 0.62; 'making': 0.63; 'protection': 0.63; 'real': 0.63; 'more': 0.64; 'different': 0.65; 'relatively': 0.65; 'spot': 0.65; 'hours': 0.66; 'here': 0.66; 'minutes': 0.67; 'six': 0.68; 'burden': 0.68; 'invalid': 0.68; 'caused': 0.69; 'unusual': 0.74; 'checks.': 0.84; 'everything,': 0.84; 'top.': 0.84; 'thing,': 0.91; 'remember,': 0.93; '2013': 0.98 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 :content-type; bh=wMJseq+vtt0ij2uPFlRN0dzaFzMfB5hqhDceruUSYK0=; b=cDfdiLCBdFRasi1m91S8Gt6Ex+OBejAktaRWXLlmQstwa4/LrxMV7X7j9ix1T6gIFt 5Hvzt9E8sd2UEFNOUkuJdOWQ2+DfG2+tcUs5Pi86iBLYHJlWntjUJCQYNp7RM3gvVmrI trlWnKIp0QPjCCd7qfZ5NTeFsrbehuE3IvtiF+MpgpuidJ1uhP6sDRL6l//gt9eq8JwK j3a+zBn5eLs9H/os/yfxeMKMrg8x2ZZOrVIidFZZ/XXVDQCYTHRidIXixZfmSoz1Nowy EwT8UT/M8gvoeexn4GD6c4g53Xh9runsrjLa0Y8dpLKFVzzaqzGTu9ZM4+0+KL4KIRX7 xZvQ== MIME-Version: 1.0 X-Received: by 10.221.68.71 with SMTP id xx7mr152802vcb.97.1375786238367; Tue, 06 Aug 2013 03:50:38 -0700 (PDT) In-Reply-To: References: Date: Tue, 6 Aug 2013 11:50:38 +0100 Subject: Re: Newbie: static typing? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 1375786241 news.xs4all.nl 15897 [2001:888:2000:d::a6]:36629 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52028 On Tue, Aug 6, 2013 at 11:28 AM, Rui Maciel wrote: > Chris Angelico wrote: >> def add_three_values(x,y,z): >> return x+y+z >> >> Do you want to test these values for compatibility? Remember, you >> could take a mixture of types, as most of the numeric types can safely >> be added. You can also add strings, or lists, but you can't mix them. >> And look! It already raises TypeError if it's given something >> unsuitable: > > If the type problems aren't caught right away when the invalid types are > passed to a function then the problem may only manifest itself in some far > away point in the code, making this bug needlessly harder to spot and fix, > and making the whole ordeal needlessly too time consuming. There are two problems that can result from not checking: 1) The traceback will be deeper and may be less clear. 2) Some code will be executed and then an exception thrown. If #2 is a problem, then you write checks in. (But be aware that exceptions can be thrown from all sorts of places. It's usually better to write your code to cope with exceptions than to write it to check its args.) But that's a highly unusual case. With >99% of Python scripts, it won't matter; programming errors are corrected by editing the source and rerunning the program from the top. (There ARE exceptions to this, but in Python they're relatively rare. In some of my serverside programming (usually in Pike), I have to code in *much* better protection than this. But if you're doing that sort of thing, you'll know.) So the real problem here is that, when there's a bug, the traceback is longer and perhaps unclear. This is at times a problem, but it's not as big a problem as the maintenance burden of all those extra type checks. You might have a bug that takes you an extra few minutes to diagnose because it's actually caused half way up the call stack (or, worse, it doesn't come up in testing at all and it slips through into production), but you save hours and hours of fiddling with the type checks, and perhaps outright fighting them when you want to do something more unusual. Or you write six versions of a function, with different type checking. Any of these scenarios is, in my opinion, far worse than the occasional bit of extra debugging work. Like everything, it's a tradeoff. And if your function signatures are sufficiently simple, you won't often get the args wrong anyway. ChrisA