Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76520
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ethan@stoneleaf.us> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.007 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'subject:not': 0.03; 'cpython': 0.05; "subject:' ": 0.07; 'string': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'objects,': 0.09; '~ethan~': 0.09; 'wrote:': 0.18; 'work,': 0.20; 'aug': 0.22; 'to:name:python-list@python.org': 0.22; 'header :User-Agent:1': 0.23; 'comparing': 0.24; 'integer': 0.24; 'mon,': 0.24; 'versions': 0.24; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'testing': 0.29; 'chris': 0.29; '(which': 0.31; '>>>>': 0.31; 'not.': 0.33; 'equal': 0.35; 'objects': 0.35; 'but': 0.35; 'false': 0.36; 'object,': 0.36; 'example,': 0.37; 'wrong': 0.37; 'two': 0.37; 'easily': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'anytime': 0.60; 'most': 0.60; 'received:173': 0.61; 'such': 0.63; 'different': 0.65; 'due': 0.66; 'answer.': 0.68; 'behavior': 0.77; '257': 0.84; 'ethan': 0.84; 'furman': 0.84; 'careful': 0.91 |
| Date | Mon, 18 Aug 2014 15:19:39 -0700 |
| From | Ethan Furman <ethan@stoneleaf.us> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 |
| MIME-Version | 1.0 |
| To | "python-list@python.org" <python-list@python.org> |
| Subject | Re: 'is not' or '!=' |
| References | <lsto1s$6gk$1@dont-email.me> <87fvgt7c4i.fsf@elektro.pacujo.net> <lstpe1$gbn$1@dont-email.me> <53F27340.90604@stoneleaf.us> <CALvWhxuXPLXwJhz-y1prvARJutP6BYpE_bbjRrH-8AmbGnnmjg@mail.gmail.com> |
| In-Reply-To | <CALvWhxuXPLXwJhz-y1prvARJutP6BYpE_bbjRrH-8AmbGnnmjg@mail.gmail.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| 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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.13118.1408400381.18130.python-list@python.org> (permalink) |
| Lines | 35 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1408400381 news.xs4all.nl 2850 [2001:888:2000:d::a6]:37636 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:76520 |
Show key headers only | View raw
On 08/18/2014 03:04 PM, Chris Kaynor wrote:
> On Mon, Aug 18, 2014 at 2:42 PM, Ethan Furman wrote:
>>
>> If you are not dealing with singletons (which is most cases), such as numbers, strings, lists, and most other
>> arbitrary objects, you will need to use "!=" or anytime the two objects you are comparing are not the exact same
>> object, you can easily get the wrong answer.
>
> For example, in CPython 3.4.1:
>>>> (254 + 3) is 257
> False
>>>> (254 + 3) == 257
> True
>>>> ('asd' + '@sd') is 'asd@sd'
> False
>>>> ('asd' + '@sd') == 'asd@sd'
> True
>
> However, when testing these cases, you need to be careful due to optimizations like smaller integer interning and string
> interning:
>
>>>> (254 + 2) == 256
> True
>>>> (254 + 2) is 256
> True
>>>> ('asd' + 'sd') == 'asdsd'
> True
>>>> ('asd' + 'sd') is 'asdsd'
> True
>
> In each of these cases, the behavior may be different in other implementations or versions of Python.
But the point to remember is that, for equal objects, '==' will work, 'is' may or may not.
--
~Ethan~
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
'is not' or '!=' "ElChino" <elchino@cnn.cn> - 2014-08-18 22:35 +0200
Re: 'is not' or '!=' MRAB <python@mrabarnett.plus.com> - 2014-08-18 21:53 +0100
Re: 'is not' or '!=' Marko Rauhamaa <marko@pacujo.net> - 2014-08-18 23:53 +0300
Re: 'is not' or '!=' "ElChino" <elchino@cnn.cn> - 2014-08-18 22:58 +0200
Re: 'is not' or '!=' Ethan Furman <ethan@stoneleaf.us> - 2014-08-18 14:42 -0700
Re: 'is not' or '!=' Ethan Furman <ethan@stoneleaf.us> - 2014-08-18 15:19 -0700
Re: 'is not' or '!=' Chris Kaynor <ckaynor@zindagigames.com> - 2014-08-18 15:04 -0700
Re: 'is not' or '!=' Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2014-08-19 11:49 +0200
Re: 'is not' or '!=' Martin S <shieldfire@gmail.com> - 2014-08-19 14:42 +0200
Re: 'is not' or '!=' Skip Montanaro <skip@pobox.com> - 2014-08-19 08:12 -0500
Re: 'is not' or '!=' Marko Rauhamaa <marko@pacujo.net> - 2014-08-19 20:29 +0300
Re: 'is not' or '!=' Tim Chase <python.list@tim.thechases.com> - 2014-08-19 12:34 -0500
Re: 'is not' or '!=' Marko Rauhamaa <marko@pacujo.net> - 2014-08-19 21:18 +0300
Re: 'is not' or '!=' Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-20 04:26 +1000
Re: 'is not' or '!=' Marko Rauhamaa <marko@pacujo.net> - 2014-08-19 21:52 +0300
Re: 'is not' or '!=' Marko Rauhamaa <marko@pacujo.net> - 2014-08-20 00:00 +0300
Re: 'is not' or '!=' Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-19 16:21 -0600
Re: 'is not' or '!=' Marko Rauhamaa <marko@pacujo.net> - 2014-08-20 02:02 +0300
Re: 'is not' or '!=' Rustom Mody <rustompmody@gmail.com> - 2014-08-20 22:03 -0700
Re: 'is not' or '!=' Marko Rauhamaa <marko@pacujo.net> - 2014-08-21 08:56 +0300
Re: 'is not' or '!=' Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-19 20:05 +0100
Re: 'is not' or '!=' Terry Reedy <tjreedy@udel.edu> - 2014-08-19 18:23 -0400
Re: 'is not' or '!=' Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-19 10:42 -0600
Re: 'is not' or '!=' Marko Rauhamaa <marko@pacujo.net> - 2014-08-19 20:36 +0300
Re: 'is not' or '!=' Skip Montanaro <skip@pobox.com> - 2014-08-19 13:44 -0500
Re: 'is not' or '!=' alister <alister.nospam.ware@ntlworld.com> - 2014-08-19 09:35 +0000
Re: 'is not' or '!=' Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2014-08-19 11:53 +0200
Re: 'is not' or '!=' Rustom Mody <rustompmody@gmail.com> - 2014-08-18 19:29 -0700
Re: 'is not' or '!=' Steven D'Aprano <steve@pearwood.info> - 2014-08-19 08:36 +0000
Re: 'is not' or '!=' Chris Angelico <rosuav@gmail.com> - 2014-08-19 18:47 +1000
Re: 'is not' or '!=' Tim Chase <python.list@tim.thechases.com> - 2014-08-19 05:36 -0500
Re: 'is not' or '!=' Ben Finney <ben+python@benfinney.id.au> - 2014-08-20 09:24 +1000
Re: 'is not' or '!=' Rustom Mody <rustompmody@gmail.com> - 2014-08-19 21:01 -0700
Re: 'is not' or '!=' Steven D'Aprano <steve@pearwood.info> - 2014-08-20 04:59 +0000
Re: 'is not' or '!=' Rustom Mody <rustompmody@gmail.com> - 2014-08-19 22:25 -0700
Re: 'is not' or '!=' Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-20 21:05 +1000
Re: 'is not' or '!=' Chris Angelico <rosuav@gmail.com> - 2014-08-20 21:17 +1000
Re: 'is not' or '!=' Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2014-08-21 20:31 +0200
Re: 'is not' or '!=' Tim Chase <python.list@tim.thechases.com> - 2014-08-20 06:26 -0500
Re: 'is not' or '!=' Chris Angelico <rosuav@gmail.com> - 2014-08-20 21:33 +1000
Re: 'is not' or '!=' Robert Kern <robert.kern@gmail.com> - 2014-08-20 12:40 +0100
Re: 'is not' or '!=' Rustom Mody <rustompmody@gmail.com> - 2014-08-20 05:01 -0700
Re: 'is not' or '!=' Dan Stromberg <drsalists@gmail.com> - 2014-08-21 12:24 -0700
Re: 'is not' or '!=' "ElChino" <elchino@cnn.cn> - 2014-08-21 21:42 +0200
csiph-web