Path: csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'interfaces': 0.04; 'static': 0.04; 'args': 0.07; 'float': 0.07; 'variables': 0.07; 'mixed': 0.09; 'structure,': 0.09; 'subject: [': 0.09; 'python': 0.11; 'def': 0.12; 'benefit.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inability': 0.16; 'iterable:': 0.16; 'sorts': 0.16; 'subject:"]': 0.16; 'language': 0.16; 'wrote:': 0.18; 'implementing': 0.19; 'thu,': 0.19; 'effort.': 0.24; 'example.': 0.24; 'lets': 0.24; 'subject:problem': 0.24; 'test.': 0.24; 'java': 0.24; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'leave': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; "d'aprano": 0.31; 'option.': 0.31; 'steven': 0.31; 'void': 0.31; 'class': 0.32; 'this.': 0.32; 'probably': 0.32; 'stuff': 0.32; 'interface': 0.32; 'says': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; 'could': 0.34; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'useful': 0.36; "i'll": 0.36; 'checks': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'does': 0.39; 'aside': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'easy': 0.60; 'then,': 0.60; 'lost': 0.61; 'subject:The': 0.64; 'worth': 0.66; 'due': 0.66; 'here': 0.66; 'close': 0.67; 'believe': 0.68; 'benefit': 0.68; 'nice,': 0.84; 'overloading': 0.84; 'self.value': 0.84; 'viable': 0.84; '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=9ZL8utvFZT+APiQ12QZ05jsP130Izk/STrPITKOxnrc=; b=N5gJuF2hEJWOQBfd5mRjruMvmCZn02Ftd08Sj2tTWDWrte3elPzt9E3e/CiZYq8QMe mr0YGuO+DDWPqukcX7elvOoOpNAGRRp8hMsmjFmGWyiJ+NPSl9Pq4rag33HqDo9WI6L3 Vr3gU6ejj+4zzVmPa86F+rii1XOtkP65n6O+Qeq+/H9IcXfW3xx2T78/yhb5lOiPf0nc MZpgjy12qBu8zSYa0f+Oyq1Cl95h2Tq4uivsgpnLvV/fzMQVEu4RkftfWOgyOzGqGfDf WvpV2sdXkxbIOrwfhT8vbUCN6e7ho/6R9zzfRlz7kfnlHm3If1m8e/RXRibD1r8MmBYW vZdQ== MIME-Version: 1.0 X-Received: by 10.220.111.133 with SMTP id s5mr21928715vcp.63.1370511949291; Thu, 06 Jun 2013 02:45:49 -0700 (PDT) In-Reply-To: <51b0565d$0$11118$c3e8da3@news.astraweb.com> References: <687dea63-84da-4c45-9366-cb5a10665d1f@googlegroups.com> <51ab95d5$0$29966$c3e8da3$5496439d@news.astraweb.com> <51ad7daf$0$11118$c3e8da3@news.astraweb.com> <31ca14e1-973d-44e6-886c-011a55755d76@googlegroups.com> <96cd7a31-40ce-4e51-9489-446b7f002a0e@googlegroups.com> <51afec46$0$29966$c3e8da3$5496439d@news.astraweb.com> <51b0565d$0$11118$c3e8da3@news.astraweb.com> Date: Thu, 6 Jun 2013 19:45:49 +1000 Subject: Re: Bools and explicitness [was Re: PyWart: The problem with "print"] 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370511951 news.xs4all.nl 15868 [2001:888:2000:d::a6]:36117 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47202 On Thu, Jun 6, 2013 at 7:29 PM, Steven D'Aprano wrote: > Whatever benefit there is in declaring the type of a function is lost due > to the inability to duck-type or program to an interface. There's no type > that says "any object with a 'next' method", for example. And having to > declare local variables is a PITA with little benefit. > > Give me a language with type inference, and a nice, easy way to keep duck- > typing, and I'll reconsider. But until then, I don't believe the benefit > of static types comes even close to paying for the extra effort. Here are some classic ways to do the multiple-types-accepted option. //C++ style: overloading int max(int a,int b) {return a>b ? a : b;} float max(float a,float b) {return a>b ? a : b;} //C++ also lets you go for templates, but leave that aside //Pike style: piped types int|float max(int|float a,int|float b) {return a>b ? a : b;} //This lets you write one lot of code but doesn't let //you declare that both args must be the same type # Python style: accept anything, then (maybe) check def max(a,b): return a if a>b else b //Pike does this too: mixed max(mixed a,mixed b) {return a>b ? a : b;} /* So does C, but only with pointers: */ void *max(void *a,void *b) {... uhh, this is nontrivial actually ...} For the "accept any object that has a next() method" sorts of rules, I don't know of any really viable system that does that usefully. The concept of implementing interfaces in Java comes close, but the class author has to declare that it's implementing some named interface. In theory there could be something that deduces the validity from the given structure, but I'm not aware of any language that does this. But it would let you do stuff like this (prototyped in Python): class Integers: def __init__(self): self.value=0 def next(self): self.value+=1 return self.value interface Iterable: next(self) def grab_three_values(Iterable iter): return iter.next(),iter.next(),iter.next() With a language that checks these sorts of things at compile time, it's not a big deal to test. With something fully dynamic like Python, it's probably not worth the effort. But maybe checks like this could be useful to something like Coverity. ChrisA