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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'arguments': 0.09; 'assumed': 0.09; 'happen.': 0.09; 'operand': 0.09; 'raises': 0.09; 'type,': 0.09; 'def': 0.12; 'added.': 0.16; 'display,': 0.16; 'easier.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'locating': 0.16; 'mixture': 0.16; "objects'": 0.16; 'pythonic': 0.16; 'reason.': 0.16; 'side.': 0.16; 'threw': 0.16; 'typeerror:': 0.16; 'types,': 0.16; 'worst': 0.16; 'java,': 0.16; 'wrote:': 0.18; 'stack': 0.19; '>>>': 0.22; 'aug': 0.22; 'issue.': 0.22; 'error': 0.23; 'instance,': 0.24; 'sort': 0.25; "i've": 0.25; 'least': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'testing': 0.29; 'am,': 0.29; 'mix': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'getting': 0.31; 'that.': 0.31; 'usually': 0.31; '"",': 0.31; 'safely': 0.31; 'file': 0.32; 'another': 0.32; '(most': 0.33; 'bugs': 0.33; 'guess': 0.33; 'programmers': 0.33; '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; 'are,': 0.36; 'c++': 0.36; 'raising': 0.36; 'surely': 0.36; 'subject:?': 0.36; 'saves': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'most': 0.60; 'skip:a 30': 0.61; 'simply': 0.61; "you're": 0.61; 'more': 0.64; 'different': 0.65; 'to,': 0.72; 'other.': 0.75; 'discovered': 0.83; 'type(s)': 0.84; 'remember,': 0.93; 'yourself,': 0.95; '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=Whvz1oQ8v5HSmp2ff38/V82QljdIzyxtlDKfyrmbdJo=; b=t1EgQG0xSsDh/8UhCsIZwUtnfHZEi8W1tStSUQtj+7ZHuQeDgcnk8FGPaTHtIHlRsw 9TrOPYXdURpKSBdxRMi0AAIttWVznihSuCXma0aPWUXJqiXgBL2H/e4aecdloK+0PKQL aTwait590c8PAQAyvlVPpnPYa7s1Z3kRxyP3JNRaRn2jaltynL9EkAJpcPcAXJyvTyge lmum1dhtybYPvBARXwJpsOxBAYo+K1e7cXXki/QykQqvGjNheN9EICsom8wmJLsfUDqK 7UgvgI/pvLb81kPYswssE0deIwAE0SLOqHCysHz30LFjMJMF7XivNeIPpzyLNUO7uBIM 5pnA== MIME-Version: 1.0 X-Received: by 10.59.9.69 with SMTP id dq5mr82121ved.87.1375781380868; Tue, 06 Aug 2013 02:29:40 -0700 (PDT) In-Reply-To: References: Date: Tue, 6 Aug 2013 10:29:40 +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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375781390 news.xs4all.nl 15896 [2001:888:2000:d::a6]:36110 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52012 On Tue, Aug 6, 2013 at 10:01 AM, Rui Maciel wrote: > It would be nice if some functions threw an error if they were passed a type > they don't support or weren't designed to handle. That would avoid having > to deal with some bugs which otherwise would never happen. > > To avoid this sort of error, I've been testing arguments passed to some > functions based on their type, and raising TypeError when necessariy, but > surely there must be a better, more pythonic way to handle this issue. 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: >>> add_three_values(1,"foo",[4,6]) Traceback (most recent call last): File "", line 1, in add_three_values(1,"foo",[4,6]) File "", line 2, in add_three_values return x+y+z TypeError: unsupported operand type(s) for +: 'int' and 'str' The Pythonic way is to not care what the objects' types are, but to simply use them. In C++ and Java, it's usually assumed that the person writing a function/class is different from the person writing the code that uses it, and that each needs to be protected from each other. In Python, it's assumed that either you're writing both halves yourself, or at least you're happy to be aware of the implementation on the other side. It saves a HUGE amount of trouble; for instance, abolishing private members makes everything easier. This philosophical difference does take some getting used to, but is so freeing. The worst it can do is give you a longer traceback when a problem is discovered deep in the call tree, and if your call stack takes more than a page to display, that's code smell for another reason. (I've seen Ruby tracebacks that are like that. I guess Ruby programmers get used to locating the important part.) ChrisA