Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103875
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| 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 | <mailman.105.1456931638.20602.python-list@python.org> (permalink) |
| References | <a894d5ed-d906-4ff7-a537-32bf0187e062@googlegroups.com> <lf5d1rdrzmz.fsf@ling.helsinki.fi> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de 9U6+flfuNOKUhpzSHlt5+waYb8y8zrOrjWZG3ee2ebVg== |
| Return-Path | <ian.g.kelly@gmail.com> |
| 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 | <lf5d1rdrzmz.fsf@ling.helsinki.fi> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:103875 |
Show key headers only | View raw
On Wed, Mar 2, 2016 at 2:35 AM, Jussi Piitulainen
<jussi.piitulainen@helsinki.fi> 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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Explaining names vs variables in Python Salvatore DI DIO <salvatore.didio@gmail.com> - 2016-03-02 00:32 -0800
Re: Explaining names vs variables in Python Jesper K Brogaard <jesper@brogAAaard.eu> - 2016-03-02 10:03 +0100
Re: Explaining names vs variables in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-02 21:32 +1100
Re: Explaining names vs variables in Python Marko Rauhamaa <marko@pacujo.net> - 2016-03-02 14:34 +0200
Re: Explaining names vs variables in Python Chris Angelico <rosuav@gmail.com> - 2016-03-02 23:50 +1100
Re: Explaining names vs variables in Python Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-02 15:11 +0200
Re: Explaining names vs variables in Python Marko Rauhamaa <marko@pacujo.net> - 2016-03-02 15:39 +0200
Re: Explaining names vs variables in Python Chris Angelico <rosuav@gmail.com> - 2016-03-03 00:48 +1100
Re: Explaining names vs variables in Python Marko Rauhamaa <marko@pacujo.net> - 2016-03-02 16:11 +0200
Re: Explaining names vs variables in Python Rustom Mody <rustompmody@gmail.com> - 2016-03-02 07:08 -0800
Re: Explaining names vs variables in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-03 04:23 +1100
Re: Explaining names vs variables in Python Rustom Mody <rustompmody@gmail.com> - 2016-03-02 09:28 -0800
Re: Explaining names vs variables in Python Marko Rauhamaa <marko@pacujo.net> - 2016-03-02 20:12 +0200
Re: Explaining names vs variables in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-03 12:52 +1100
Re: Explaining names vs variables in Python Rustom Mody <rustompmody@gmail.com> - 2016-03-03 09:03 -0800
Re: Explaining names vs variables in Python Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-03 12:53 -0700
Re: Explaining names vs variables in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-02 21:49 +0000
Re: Explaining names vs variables in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-03 13:05 +1100
Re: Explaining names vs variables in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-03 16:09 +0000
Re: Explaining names vs variables in Python Chris Angelico <rosuav@gmail.com> - 2016-03-03 08:52 +1100
Re: Explaining names vs variables in Python Rustom Mody <rustompmody@gmail.com> - 2016-03-02 17:23 -0800
Re: Explaining names vs variables in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-02 22:51 +0000
Re: Explaining names vs variables in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-03 04:10 +1100
Re: Explaining names vs variables in Python Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-02 10:08 +0100
Effects of caching frequently used objects, was Re: Explaining names vs variables in Python Peter Otten <__peter__@web.de> - 2016-03-02 10:12 +0100
Re: Explaining names vs variables in Python Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-02 11:35 +0200
Re: Explaining names vs variables in Python Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-02 08:13 -0700
Re: Explaining names vs variables in Python Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-02 17:37 +0200
Re: Explaining names vs variables in Python Steven D'Aprano <steve@pearwood.info> - 2016-03-02 21:16 +1100
Re: Explaining names vs variables in Python "ast" <nomail@invalid.com> - 2016-03-02 11:52 +0100
Re: Explaining names vs variables in Python Salvatore DI DIO <salvatore.didio@gmail.com> - 2016-03-02 02:58 -0800
Re: Effects of caching frequently used objects, was Re: Explaining names vs variables in Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-03-02 09:16 -0500
Re: Explaining names vs variables in Python Ben Finney <ben+python@benfinney.id.au> - 2016-03-03 04:53 +1100
RE: Effects of caching frequently used objects, was Re: Explaining names vs variables in Python Albert-Jan Roskam <sjeik_appie@hotmail.com> - 2016-03-25 13:03 +0000
Re: Effects of caching frequently used objects, was Re: Explaining names vs variables in Python Chris Angelico <rosuav@gmail.com> - 2016-03-26 00:22 +1100
Re: Effects of caching frequently used objects, was Re: Explaining names vs variables in Python Ethan Furman <ethan@stoneleaf.us> - 2016-03-25 09:45 -0700
csiph-web