Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'syntax': 0.04; 'argument': 0.05; 'subject:Python': 0.06; 'advocate': 0.07; 'float': 0.07; 'arguments': 0.09; 'decorator': 0.09; 'difference,': 0.09; 'lines.': 0.09; 'mind,': 0.09; 'parameter': 0.09; 'satisfy': 0.09; 'section,': 0.09; 'things,': 0.09; 'type,': 0.09; 'python': 0.11; 'def': 0.12; '*function': 0.16; '2);': 0.16; 'agree.': 0.16; 'annotations.': 0.16; 'better:': 0.16; 'clashes': 0.16; 'declarations': 0.16; 'int)': 0.16; 'rationale': 0.16; 'separated': 0.16; 'syntax,': 0.16; 'two.': 0.16; 'worse.': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; '>>>': 0.22; 'import': 0.22; 'putting': 0.22; 'separate': 0.22; 'tests': 0.22; 'aspect': 0.24; 'closely': 0.24; 'decorators': 0.24; 'java': 0.24; 'together.': 0.24; 'file.': 0.24; 'sort': 0.25; 'right.': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'feature': 0.29; 'related': 0.29; "doesn't": 0.30; 'newer': 0.30; 'especially': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; 'easier': 0.31; 'lines': 0.31; 'assert': 0.31; 'closer': 0.31; "d'aprano": 0.31; 'minor': 0.31; 'pascal': 0.31; 'steven': 0.31; 'there.': 0.32; 'this.': 0.32; 'languages': 0.32; '(including': 0.33; 'url:python': 0.33; 'everyone': 0.33; 'actual': 0.34; 'sense': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; 'basic': 0.35; "can't": 0.35; 'except': 0.35; 'skip:s 30': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'are,': 0.36; 'much.': 0.36; 'var': 0.36; 'url:listinfo': 0.36; 'next': 0.36; 'url:org': 0.36; 'should': 0.36; 'unit': 0.37; 'two': 0.37; 'expressed': 0.37; 'from:addr:googlemail.com': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'rather': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'ensure': 0.60; 'easy': 0.60; 'tell': 0.60; 'name:': 0.61; 'further': 0.61; 'first': 0.61; 'information': 0.63; 'name': 0.63; 'myself': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'different': 0.65; 'between': 0.67; 'close': 0.67; 'detail.': 0.68; 'apart': 0.72; 'repeat': 0.74; 'again?': 0.84; 'body:': 0.84; 'idiom': 0.84; 'subject:Solution': 0.84; 'do:': 0.91; 'subject:Proposal': 0.91; 'anywhere,': 0.93; 'poorly': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=P0XRBm0BxR+5Z+7ddVroxWe/YRU7S/GZwdK8AhzxNxk=; b=aKWbNism+9h7Fv3LxG9yXeFvbjx6H+MVzkc486XdqLj/q7RaFGwUhlvIIFUPahl8Hf u9xCXFDUazi5SrmZEDrQ6469xm5Hlrr+j2neMeZzf+LR8N5TyPtY43OY896A2O2zvdCF f5A5fo83lWEXueKHKiE2GyFctqLdI/covrUrd0Bw/SolQh1brEEBdWxBvPTIQULFZWwp zEazihl0kfuqTVxLHBD3zfp1TxDrYbx9S3lRJTbPRAMnDi/UK75tcA1BKilL31YEgygg ByeY5uDeulM73WK3qb74P6yhmHnvbHfHOU2wYOePIPVjIyK8OH8x1sHeoNMOFK4pMedV lGcA== MIME-Version: 1.0 X-Received: by 10.140.30.197 with SMTP id d63mr25616960qgd.13.1422133276889; Sat, 24 Jan 2015 13:01:16 -0800 (PST) In-Reply-To: <54c339d7$0$13005$c3e8da3$5496439d@news.astraweb.com> References: <6da1eb58-a0bb-4d37-8293-0a8cafe6a89c@googlegroups.com> <54c1e349$0$12975$c3e8da3$5496439d@news.astraweb.com> <54c339d7$0$13005$c3e8da3$5496439d@news.astraweb.com> Date: Sat, 24 Jan 2015 22:01:16 +0100 Subject: Re: Python Sanity Proposal: Type Hinting Solution From: "Fetchinson ." 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: 158 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422133285 news.xs4all.nl 2839 [2001:888:2000:d::a6]:36475 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84491 On 1/24/15, Steven D'Aprano wrote: > Fetchinson . wrote: > >> On 1/23/15, Steven D'Aprano wrote: > [...] >>> Cobra is especially close to Python-like syntax, and supports unit tests >>> as well: >>> >>> >>> def sqroot(i as int) as float >>> require >>> i > 0 >>> ensure >>> result > 0 >>> tests >>> assert sqroot(25) == 5.0 >>> body >>> ... >>> >>> It would be nice to be able to include at least *some* tests right there >>> in the code rather than in a separate file. >> >> I completely agree. A cobra-style type hinting implementation would >> satisfy everyone who doesn't want to make function signatures noisy >> (including me) and would also satisfy those who advocate for the >> actual feature (type hinting) without worrying much about the >> implementation detail. These two groups are overlapping by the way :) > > I don't understand this. Cobra's type-hints are right there in the function > signature, just like Python annotations. > > > # Cobra > def sqroot(i as int) as float > > # Python > def sqroot(i:int)->float: You are right. This aspect is pretty close, what I had in mind, but expressed myself poorly is this idiom in cobra: def myfunc( a, b ) require something_about( a, b ) ensure something_about_the_return_value Yes, type hinting and arbitrary constraints on the function arguments and return types are different things, but closely related. I'd say it makes sense to combine the two. > > Cobra's use of "as" clashes with Python. In Python, "as" is used for > name-binding: > > import module as name > with open('file') as f > except Exception as e > > but apart from that minor difference, they're virtually identical. > > >> In any case, I'm pretty sure it was said before, but I can't really >> find it anywhere, can someone tell me what the rationale is for >> *function signature* type hinting? > > The basic principle is that things which are related should be found > together. The further away they are, the worse. > > Bad: > - the parameter name and the type are in different files > > Better: > - the parameter name and the type are only a few lines apart > > Best: > - the parameter name and type are right next to each other > > > The closer they are, the easier it is to keep them in sync, and the easier > it is to see the relevant information at a glance. Putting them together > also means that you don't have to repeat the argument name: > > int n > def spam(n): ... > > versus > > def spam(n:int): ... > > > Those reasons are why decorators have the syntax which they do: > > @decorator > def spam(n): > do_this() > do_that() > do_something_else() > > > is better than the old way of using decorators: > > def spam(n): > do_this() > do_that() > do_something_else() > > spam = decorator(spam) > > > The decorator is only a single line away from the signature, and you don't > have to repeat the name. > > > We can see this at work in Pascal. Pascal functions have type declarations > in the signature, and variable declarations between the signature and the > body: > > > function sqroot(arg: Integer): Real; > var > x: Integer; > y: Real; > z: Something_Else; > begin > do_this(1, 2); > do_that(3, 4); > do_something_else(5, 6); > x := some expression; { what's the type of x again? } > end; > > > The declarations in the signature work very well and are easy to use, but > the "var" section, not so much. Especially in large functions, the place > where you declare the variable and its type, and the place where you first > use it, can be separated by many lines. This makes maintenance and reading > of the code more difficult. > > Newer languages like Java let you declare the variable the first time you > use it: > > int x = some expression; > > and you don't have to search very far to find out what sort of thing x is, > it is right there. > > > > -- > Steven > > -- > https://mail.python.org/mailman/listinfo/python-list > -- Psss, psss, put it down! - http://www.cafepress.com/putitdown