Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'static': 0.04; 'anyway.': 0.05; 'executed': 0.09; 'falls': 0.09; 'raises': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'runs': 0.10; 'def': 0.12; 'bug': 0.12; 'jan': 0.12; 'added.': 0.16; 'clear.': 0.16; 'contagious.': 0.16; 'iteration': 0.16; 'iterators': 0.16; 'looping': 0.16; 'manifest': 0.16; 'mixture': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'types,': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'passing': 0.19; '>>>': 0.22; 'code,': 0.22; 'memory': 0.22; 'aug': 0.22; 'issue.': 0.22; 'header:User-Agent:1': 0.23; "aren't": 0.24; 'visible': 0.24; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'mix': 0.30; 'code': 0.31; 'implicit': 0.31; 'safely': 0.31; 'checking': 0.33; 'could': 0.34; 'problem': 0.35; "can't": 0.35; 'something': 0.35; 'usual': 0.35; 'test': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'subject:?': 0.36; 'too': 0.37; 'two': 0.37; 'problems': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'rather': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'eventually': 0.60; 'most': 0.60; 'received:173': 0.61; 'making': 0.63; 'skip:n 10': 0.64; 'spot': 0.65; 'invalid': 0.68; 'float,': 0.84; 'received:fios.verizon.net': 0.84; 'careful': 0.91; 'remember,': 0.93; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Newbie: static typing? Date: Tue, 06 Aug 2013 19:02:34 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 In-Reply-To: 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375830173 news.xs4all.nl 15919 [2001:888:2000:d::a6]:42772 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52079 On 8/6/2013 6:50 AM, Chris Angelico wrote: > 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. 3) The code falls into an infinite loop or recursion. The solution is to think before looping or recursing. This often involves value checking (non-negative int or non-fractional float, for instance) rather than type checking in the usual static type-checking sense. One also needs to be careful about passing unbounded iterators to other functions and remember that unboundedness is contagious. (filter(pred, unbounded_iterator) is an unbounded iterator). Again, this is a 'value' or implicit sub-type issue rather than a explicit, visible 'type' issue. Infinite recursion will be caught eventually when memory runs out (and that is an advantage of recursion over iteration ;-), but I prefer to avoid it anyway. -- Terry Jan Reedy