Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?) Date: Sun, 13 Mar 2016 00:06:34 +1100 Lines: 54 Message-ID: References: <87h9gcxkd3.fsf@elektro.pacujo.net> <56e40b7b$0$1611$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de nsy8XG26V3pTERVkX/ZfSwxFAZPLAqFUifqC9/R708BQ== 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; 'received:209.85.223': 0.03; 'cpython': 0.05; "'a'": 0.07; 'compile-time': 0.07; 'cc:addr :python-list': 0.09; 'subject:which': 0.09; 'way:': 0.09; 'whichever': 0.09; 'python': 0.10; 'python.': 0.11; '2016': 0.16; 'chris,': 0.16; 'folding.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'instead:': 0.16; 'integers,': 0.16; 'literal.': 0.16; 'need,': 0.16; 'ordinals': 0.16; 'peephole': 0.16; 'py3': 0.16; 'really?': 0.16; 'received:209.85.223.173': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:?)': 0.16; 'subscripting': 0.16; 'unicode.': 0.16; 'wrote:': 0.16; 'string': 0.17; 'byte': 0.18; 'integer': 0.18; 'language': 0.19; '>>>': 0.20; 'versions': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'ascii': 0.22; 'constant': 0.22; 'disable': 0.22; 'sat,': 0.23; 'this:': 0.23; 'unlike': 0.23; 'header:In-Reply-To:1': 0.24; 'feature': 0.24; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'necessary,': 0.29; 'yields': 0.29; 'call.': 0.30; '"the': 0.32; "d'aprano": 0.33; 'steven': 0.33; 'though.': 0.33; 'true.': 0.33; 'values.': 0.33; 'definition': 0.34; 'received:google.com': 0.35; 'text': 0.35; 'unicode': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'say': 0.37; '12,': 0.37; 'doing': 0.38; 'difference': 0.38; 'received:209': 0.38; 'rather': 0.39; 'future': 0.60; 'subject:The': 0.61; 'complete': 0.63; 'between': 0.65; 'mar': 0.65; '>>>>>': 0.66; 'here': 0.66; 'lack': 0.76; 'miss': 0.77; 'chrisa': 0.84; 'to:none': 0.91; 'safe.': 0.93 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; bh=hGyyMtZjsSidf1p6yyRMKh+7it464MLFVH8hCnqTtds=; b=Rx45xqMyi3NyOrkL5iZvL584m/0UTeOhv/JhCvwP/BSlvd1VwFKhJvKyV5BpgUpxfu vlrYalCgz4uWln/VmIhuKMxKJUbZE/Isr+y/u67wmb2tVNhelys31G9/625BF/M+nzza EhS8fi506UQvPBWJZ81XLue9EKOQlJrp3RYLFL2x12R4OBihCmm0uHrVj1OvSH+KuSra wmQRvpbEBW6uy1Q3LH0KR5DtGZTTS8rrf21VZdvgqDw0/eCgDTIw2Y55TPsxaAVnYzOs T3B8L2jAuFyW3CaOZR+3COmBW8LwxaEMZjhnOicxhRBGWPDvhXR/Uw24hzZvTAY3qnzx CKNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=hGyyMtZjsSidf1p6yyRMKh+7it464MLFVH8hCnqTtds=; b=T/3B8bwl47Z+e0yyQecregyWB3W1+CRkubljRaHOJMf6Wx33n5Vbny8Z3p7rCasu8T fwhppcuNQlyD+rM0ylD5Ldic60gzsuCwwAMspyntRue2vB/IaH0bTsLKSwzGRfcLQtQy HuXZySTZJzIkSJzcRNwzuooGgUsbg/s2V5GzvvT2TaKGgvNE8PnR95MY3eXZ6qSWgXQI ZJTuVYaOM451KdXjCxUCC0ujiVKhcZRBynCjIS48ukueOfHMhFlxiRknunfFEu1UPXw3 TRnWHabJRzrBGNeNyZhX/+8f32QS6uvQIBY1Ouf/KEwkAPzVIuSJdH7X2iw6pJIFcRuD Ta+g== X-Gm-Message-State: AD7BkJI+1hG36GMZ8lepqf1JHgjHUATu3ZLQPCj3byZW4HGARhT19ndcsx9iRnF55EmrGiMOSuOjEu1wY4SVqg== X-Received: by 10.107.47.163 with SMTP id v35mr14839538iov.19.1457787994450; Sat, 12 Mar 2016 05:06:34 -0800 (PST) In-Reply-To: <56e40b7b$0$1611$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104699 On Sat, Mar 12, 2016 at 11:28 PM, Steven D'Aprano wrote: > On Sat, 12 Mar 2016 11:10 pm, Chris Angelico wrote: > >> On Sat, Mar 12, 2016 at 10:08 PM, BartC wrote: >>>>> You're not mistaken. There are no "character constants" in Python. >>>>> (Note that the definition would be Unicode codepoints, rather than >>>>> ASCII values.) I don't often miss them, though. >>> >>>> Yes, a complete non-issue. >>> >>> >>> Really? The issue as I see it is this: >>> >>> Writing: a=65 generates this byte-code for the right-hand-side: >>> >>> LOAD_CONST 1 (65) An integer >>> >>> But writing instead: a=ord('A') generates this: >>> >>> LOAD_GLOBAL 0 (ord) >>> LOAD_CONST 1 ('A') A string >>> CALL_FUNCTION 1 >> >> I think the "non-issue" here is the difference between ASCII and >> Unicode. Either way, there's no way to say "the integer with the >> codepoint of this character" as a literal. But that's actually not >> even all that necessary, because subscripting a text string yields >> one-character strings - you almost never need the ordinals. >> >> Subscripting a byte string in Py3 yields integers, so you might need >> ordinals for ASCII byte values. But you can get them the same way: >> >>>>> dis.dis(lambda: b"a"[0]) >> 1 0 LOAD_CONST 3 (97) >> 3 RETURN_VALUE >>>>> dis.dis(lambda: u"a"[0]) >> 1 0 LOAD_CONST 3 ('a') >> 3 RETURN_VALUE >> >> Whichever one you need, you can get as a compile-time constant. > > Chris, what you're looking at is the result of the CPython keyhole optimizer > doing constant folding. That is **NOT** a language promise. Other > implementations of Python may lack the keyhole optimizer. Future versions > may remove it, or allow the user to disable it. > > Python the language has no feature that *guarantees* that you can write 'a' > and get 97 as a compile-time constant. Very true. However, the peephole optimizer MUST use only what can be compile-time optimized - unlike a function call. So in terms of optimizations, it's proof that it's safe. ChrisA