Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Explaining names vs variables in Python Date: Wed, 2 Mar 2016 08:13:16 -0700 Lines: 50 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 9U6+flfuNOKUhpzSHlt5+waYb8y8zrOrjWZG3ee2ebVg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'subject:Python': 0.05; 'none,': 0.05; "'',": 0.07; 'none)': 0.07; 'interpreter,': 0.09; 'index': 0.13; 'wed,': 0.15; '2016': 0.16; 'constants': 0.16; 'delicate': 0.16; 'different,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'mechanism': 0.18; '>>>': 0.20; 'constant': 0.22; 'object.': 0.22; 'suppose': 0.22; 'am,': 0.23; 'appears': 0.23; 'code.': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'earlier': 0.27; 'message-id:@mail.gmail.com': 0.27; "skip:' 10": 0.28; 'block,': 0.29; 'code': 0.30; 'guess': 0.31; "can't": 0.32; 'compiled': 0.32; 'idle': 0.33; 'smart': 0.33; 'received:google.com': 0.35; 'could': 0.35; 'false': 0.35; 'but': 0.36; 'too': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'enough': 0.39; 'to:addr:python.org': 0.40; 'mar': 0.65; 'of:': 0.66; 'reuse': 0.66; "they're": 0.66; 'results.': 0.67; 'evaluate': 0.72; 'to:name:python': 0.84; '800': 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; bh=el8ZhNmRiXeSg6ZhpRucCm/zWsV8XcKm7Hlngsg5/v0=; b=ZhtvhmkhHat1ubAy1sE6lOFadaRC6XFpc3hJwIwe7YGzIyI/dngnC4vvLmZHQDLeJ9 awb5d3HkOB3VAXc8hnSqil+7C8gi5I2ylOMaAfxfGh1fTopSU5xBmivoxuXMl3Mk4l78 msNhNxFhcwkp/bj5k2apAksc+m3JpEGM5vgRxHRGrPa08kHUXnt8TZDexRH6QkL6smlF D9qLcGZZpBlhq72L8/LaRu+gJ2f7BqrK9dEyR/9kaBOSfvlotFvJxmdpXNZaZaBttmhP 0ecS5qQwOgUSi5kkKH0VrCMHzAPe7uho/1ZTRoLI56ML5JuLqDafAvKq09XwNOPsf7/0 aErw== 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:from:date :message-id:subject:to; bh=el8ZhNmRiXeSg6ZhpRucCm/zWsV8XcKm7Hlngsg5/v0=; b=etB0/SAjuU4ZqtONlExl9HHx/ffxnyZvfEpaoPHZB842t+VLFt8s8sPdjKidF58DsX JkNc4HGnOxTYGMYeAVYr53Z/xNCfLzHeQYaU6FKjdiLojwsYvIEwnZZeTwkoFVi9YUWc z0IwSlJeHCutQiHNFeFvSrheJV2j2be9OCdxPEvI/33GmTT09wxAotton95/Px2Vp6uU JQXme1xKlur9YEvkJgFGUbjU4kt77CDE7naAkG5rJiDB6dlpUF//kSNZVYSz70gqp212 yhR7ohxjV5wTQBLoA/BfCZxIPa0ypksyBVZdFiOvI2wkLqr6o1gZk1b/9Y8Ze319g7ZL 48+g== X-Gm-Message-State: AG10YOQ5QhDjJkd9C/rzUoLsxiI4iulJ5DfyIm60TFbjuyPaeuKYLlHfgT5SIpcj4Jgtq59y81kQuVXvlTfp+w== X-Received: by 10.107.185.214 with SMTP id j205mr31276106iof.111.1456931635786; Wed, 02 Mar 2016 07:13:55 -0800 (PST) In-Reply-To: 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:103875 On Wed, Mar 2, 2016 at 2:35 AM, Jussi Piitulainen wrote: > The following are too delicate for me. I suppose the answers could have > been different, but I can't guess what mechanism actually leads to these > results. Just idle curiosity on my part. > >>>> 890 is 890 > True >>>> id(890) == id(890) > True This has to do with the way code blocks are compiled. In the interactive interpreter, a single line like '890 is 890' is compiled to a single code object. The constant 890 appears twice in the same code block, so the optimizer uses the same constant for both. Note in the following that the same index appears for both, so they're actually the same object reference. >>> import dis >>> dis.dis('890 is 890') 1 0 LOAD_CONST 0 (890) 3 LOAD_CONST 0 (890) 6 COMPARE_OP 8 (is) 9 RETURN_VALUE >>> compile('890 is 890', '', 'exec').co_consts (890, None) As for the earlier example of: >>>> 1 + 1 is 2 > True >>>> 800 + 90 + 0 is 890 > False This one actually surprises me a little, because the optimizer is also smart enough to evaluate '800 + 90 + 0' and just store a constant of 890: >>> dis.dis('800 + 90 + 0 is 890') 1 0 LOAD_CONST 5 (890) 3 LOAD_CONST 3 (890) 6 COMPARE_OP 8 (is) 9 RETURN_VALUE >>> compile('800 + 90 + 0 is 890', '', 'exec').co_consts (800, 90, 0, 890, None, 890, 890) Not smart enough to reuse the existing reference in this case apparently, or even to prune out the original constants that are no longer used in the code.