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; 'example:': 0.03; 'static': 0.04; 'subject:Python': 0.06; 'compiler': 0.07; 'float': 0.07; 'ignored': 0.07; 'interpreter.': 0.07; '%s"': 0.09; 'annotations': 0.09; 'arguments': 0.09; 'check,': 0.09; 'compile-time': 0.09; 'pep': 0.09; 'runtime': 0.09; 'wrapper': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; '%s,': 0.16; '11:42': 0.16; 'arg': 0.16; 'arg,': 0.16; 'declarations': 0.16; 'expecting': 0.16; 'meanwhile,': 0.16; 'positional': 0.16; 'statement.': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'skip:f 30': 0.19; 'unlike': 0.19; '>>>': 0.22; 'input': 0.22; 'import': 0.22; 'lets': 0.24; 'skip:l 30': 0.24; 'sort': 0.25; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; '(this': 0.29; 'am,': 0.29; 'raise': 0.29; 'errors': 0.30; 'evaluation': 0.30; 'skip:@ 10': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '"",': 0.31; 'ctypes': 0.31; 'int,': 0.31; 'noted': 0.31; 'file': 0.32; 'interface': 0.32; '(most': 0.33; 'fri,': 0.33; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'easily': 0.37; 'expected': 0.38; 'implement': 0.38; 'checks': 0.38; 'to:addr:python-list': 0.38; 'previous': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'affect': 0.61; 'extended': 0.61; 'course': 0.61; 'simply': 0.61; 'simple': 0.61; 'more': 0.64; 'different': 0.65; '30,': 0.65; 'optimized': 0.68; 'subject:! ': 0.74; 'yourself': 0.78; '2015': 0.84; 'evaluation.': 0.84; 'execution.': 0.84; 'warnings.': 0.84; 'gladly': 0.91; 'whereas': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=KWBiu9qKwnEjj3UKNEvB6Dfrr9WfRAFtwy232jrprk8=; b=l2W+mJbbmb62+sj7QKG5L98efpquYo2JzvteekHZhhkar0+zGeXWxeQvPtzRT7ZYR9 ZMK/AMRtOnyw7h9CRuAEHGBF+iFUUuDCvtQP0T1bs6S78cW+ql1AH8b3NEn808PoH0FX G6aoQpU88FjmZW71pkN/RKy0eTQv1fT1hWHXO+JXLZ2WxshrNek8S94xJYm/5dkNrjGt g0hvqudbYuZuNepq8joEqP9UhSTms5We8gd0zS6UW1YdTa0t3XkW2LFbpRIcfyRU3GQP uPOezVufO14hPYZvEN60v1Ld5AJZCtfpf/xKlnoHNnmryG3Swmi+JRTRyoBSuWtSRTLC 9Kyg== X-Received: by 10.70.133.200 with SMTP id pe8mr11035383pdb.89.1422654663494; Fri, 30 Jan 2015 13:51:03 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: <54c07d04$0$13012$c3e8da3$5496439d@news.astraweb.com> <1939680766443642112.878725sturla.molden-gmail.com@news.gmane.org> <6eb91c4b-92ff-44a8-b5a9-6ef04c71f4cb@googlegroups.com> <35a40ec6-3763-448b-9ea4-4a233a04979b@googlegroups.com> <54c1ccc8$0$13005$c3e8da3$5496439d@news.astraweb.com> <54c6d7c2$0$12992$c3e8da3$5496439d@news.astraweb.com> <54c83ab4$0$12982$c3e8da3$5496439d@news.astraweb.com> <54ca5bbf$0$12992$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Fri, 30 Jan 2015 14:50:23 -0700 Subject: Re: Python is DOOMED! Again! To: Python Content-Type: text/plain; charset=UTF-8 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422654667 news.xs4all.nl 2850 [2001:888:2000:d::a6]:37275 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:84920 On Fri, Jan 30, 2015 at 11:42 AM, Mario Figueiredo wrote: > To be clear, type declarations in Julia, Scala, C have the potential to > produce side-effects, can result in optimized code and can result in > compile time errors or warnings. They also affect runtime evaluation as > you could easily attest if you input a float into a function expecting > an int, whereas in Python the float will be gladly accepted and will > only fail at the point in code where its interface won't match the > statement. At least for C, as I noted in a previous post, it is simply not true that they are used for runtime evaluation. For example: >>> import ctypes >>> libc = ctypes.CDLL("libc.so.6") >>> libc.abs(ctypes.c_double(123.456)) 2093824448 The C compiler may complain about it, but that's a compile-time static check, no different from the sort of checks that PEP 484 seeks to add to Python. > Meanwhile, type annotations in Python are instead completely ignored by > the interpreter. They do nothing of the above. They do not participate > in code generation and execution. But unlike C, Python lets you easily implement this yourself if you want to. >>> def runtime_type_check(f): ... @functools.wraps(f) ... def wrapper(**args): ... for arg, value in args.items(): ... if arg in f.__annotations__: ... if not isinstance(value, f.__annotations__[arg]): ... raise TypeError("Arg %s expected %s, got %s" ... % (arg, f.__annotations__[arg].__name__, type(arg).__name__)) ... return f(**args) ... return wrapper ... >>> @runtime_type_check ... def add(x:int, y:int) -> int: ... return x + y ... >>> add(x="hello", y="world") Traceback (most recent call last): File "", line 1, in File "", line 8, in wrapper TypeError: Arg y expected int, got str (This could of course be extended for positional arguments and more complex annotations, but I wanted to keep it simple for the purpose of the example.)