Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'args': 0.07; 'binary': 0.07; 'float': 0.07; 'revision': 0.07; 'transform': 0.07; 'string': 0.09; 'assuming': 0.09; 'exist,': 0.09; 'parsed': 0.09; 'parsing': 0.09; 'subject:language': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; "(i'm": 0.16; '6:52': 0.16; 'ast': 0.16; 'calculator': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hmm.': 0.16; 'literals': 0.16; 'literals,': 0.16; 'nodes': 0.16; 'notation,': 0.16; 'subject: \n ': 0.16; 'syntax,': 0.16; 'typeerror:': 0.16; 'way;': 0.16; 'wrote:': 0.18; 'discussion': 0.18; 'not,': 0.20; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'necessary.': 0.24; 'replace': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'possibly': 0.26; 'supported': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; '(which': 0.31; '"",': 0.31; '(maybe': 0.31; 'branches': 0.31; "d'aprano": 0.31; 'decimal': 0.31; 'info.': 0.31; 'steven': 0.31; 'file': 0.32; '(most': 0.33; 'fri,': 0.33; 'maybe': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'common': 0.35; 'point.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'done,': 0.36; 'doubt': 0.36; 'idle': 0.36; 'done': 0.36; 'possible': 0.36; 'should': 0.36; 'turn': 0.37; 'easiest': 0.38; 'skip:m 40': 0.38; 'pm,': 0.38; 'previous': 0.38; 'short': 0.38; 'recent': 0.39; 'help,': 0.39; 'changed': 0.39; 'entire': 0.61; 'strictly': 0.61; 'simple': 0.61; 'first': 0.61; 'back': 0.62; 'such': 0.63; 'happen': 0.63; 'relatively': 0.65; 'skip:m 50': 0.68; 'default': 0.69; 'subject:this': 0.83; 'to:none': 0.92; 'serious': 0.97 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:cc :content-type; bh=aXuzRf3l+WJwukrGKilzf67nf/UlwrfLY4pv7pyk4BA=; b=ywuza2uoYlTv4hdHwbpLXOIVcF+bLvOa5KybkUHzCPSB470kBGt0Kcn1wgfKO5fym4 h5p6ejkFie+paVHoESD7bbXmXAU5XcNbSK2YWj3vxGwfuMQeiEIqEbdu0htmNHPFk0Ix kU99THZ7Ej6rGjgqElUWvFkAkbLRoKPcYmlfXLRpO3kGQzQsSArQVPxe1yPK7Pw6xq4j en8eOE46ngPO/F9rktUMcyxrPgnhAWkodLgDcYPa9VChR1IfoqvBGscMM49YB4IaAQ2j n8nGctxV2Yh207ctxRt1jTVN6SKhnQk4v63k0YPnDUQmldIM/N6z3d3MWjY8yI13Y7DG i4HQ== MIME-Version: 1.0 X-Received: by 10.69.31.74 with SMTP id kk10mr2636459pbd.169.1396599100269; Fri, 04 Apr 2014 01:11:40 -0700 (PDT) In-Reply-To: <533e64ce$0$29993$c3e8da3$5496439d@news.astraweb.com> References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <87ior3w740.fsf@elektro.pacujo.net> <5334c38e$0$29994$c3e8da3$5496439d@news.astraweb.com> <53364327$0$29994$c3e8da3$5496439d@news.astraweb.com> <53365F55.2040302@gmail.com> <533836c4$0$29994$c3e8da3$5496439d@news.astraweb.com> <533e64ce$0$29993$c3e8da3$5496439d@news.astraweb.com> Date: Fri, 4 Apr 2014 19:11:40 +1100 Subject: Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list) From: Chris Angelico Cc: "python-list@python.org" 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396599103 news.xs4all.nl 2842 [2001:888:2000:d::a6]:38526 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69651 On Fri, Apr 4, 2014 at 6:52 PM, Steven D'Aprano wrote: > py> x = Decimal("0.7777777777787516") > py> y = Decimal("0.7777777777787518") > py> (x + y) / 2 > Decimal('0.7777777777787515') > > I've changed my mind about Python using Decimal as the default numeric > type. I think that would send a very strong message that Python is not > for serious numeric work. Hmm. Good point. (I'm not familiar with your notation, by the way; assuming x...y includes both endpoints? The first thing that comes to mind is git's revision syntax, in which x and y would be tags/commits/branches and that would give you everything that's in those branches but not in their common parent. And I doubt very much that's what you mean!) So I go back to a previous form of the request/desire: that the AST store numeric literals as their original strings (maybe as well as the parsed version), such that an AST transform can replace all float literals with Decimal literals (if they exist, or calls to Decimal and string args if they don't). >>> print(ast.dump(ast.parse("print(0.1 + 0.2)"))) Module(body=[Expr(value=Call(func=Name(id='print', ctx=Load()), args=[BinOp(left=Num(n=0.1), op=Add(), right=Num(n=0.2))], keywords=[], starargs=None, kwargs=None))]) Possibly the easiest way - and maybe I should shift this part of the discussion to -ideas - would be to have Num nodes retain additional meta-information, in the same way that nodes retain line/column info. Then it would be relatively simple to write a "Decimal-mode" hook for IDLE that would turn it into a Decimal calculator instead of a binary float calculator. Adding Decimal as a supported type (which would have to happen if Python gets Decimal literals, but could be done in other ways too) would help, though it's not strictly necessary. >>> mod=ast.parse("print(0.1 + 0.2)") >>> exec(compile(mod,"-","exec")) 0.30000000000000004 >>> mod.body[0].value.args[0].left.n=Decimal("0.1") >>> mod.body[0].value.args[0].right.n=Decimal("0.2") >>> exec(compile(mod,"-","exec")) Traceback (most recent call last): File "", line 1, in exec(compile(mod,"-","exec")) TypeError: non-numeric type in Num But it can still be done, as long as it's possible to go back to the original string - which currently it's not, short of parsing the entire Python program manually. ChrisA