Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101847

Re: "x == None" vs "x is None"

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: "x == None" vs "x is None"
Date Sun, 17 Jan 2016 22:29:57 +1100
Lines 45
Message-ID <mailman.59.1453030200.15297.python-list@python.org> (permalink)
References <n7fo7q$dso$1@news2.informatik.uni-stuttgart.de> <mailman.57.1453025729.15297.python-list@python.org> <n7fshb$f36$1@news2.informatik.uni-stuttgart.de>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 1AzaGJOOFngtnI4pE8fegA43aH8bosfdB/MNOcAQhLbQ==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'compiler': 0.05; 'none)': 0.07; 'cc:addr:python-list': 0.09; '"if': 0.09; 'check.': 0.09; 'none"': 0.09; 'subject:None': 0.09; 'jan': 0.11; 'times,': 0.13; '"is': 0.16; '10:05': 0.16; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'partly': 0.16; 'personally,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'header:In-Reply-To:1': 0.24; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; 'actual': 0.28; "i'd": 0.31; 'received:google.com': 0.35; 'false': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'subject:" ': 0.36; 'pm,': 0.36; 'received:209.85.213': 0.37; 'received:209': 0.38; 'several': 0.38; 'rather': 0.39; 'term': 0.60; 'more': 0.63; 'within': 0.64; 'places': 0.64; '(is': 0.84; 'chrisa': 0.84; 'not)': 0.84; 'to:none': 0.91; 'spell': 0.91; 'why?': 0.91
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=GokLw8k4KTrWUHiyGkaPsEb+eTNbJwdgCbTxGIZxehE=; b=S/tYfJW6VgvWLO5GrISLmDKDZ0/u7Xg1Se30NWL0A0+nvt6JSgCj4m0w1H9SMQ62Gz p6Cj7F6tDkfbOPHwhhwMNxMHpuOL78nebfdMfjIPUtrTfgeJv4ZqE4uZsRE6N8wK3Gkb KKRhRdGQIfuPyeDJU4dS2l32VXo6NejCSMuB42vWm75fGbx+Lv1x5F3c1pEKYkPFGCDt WYEUjl+X7uUDzK6ArvxNlzPI//9vPF1n31E6wq6Qvlr0YzJmp7SoegisXvRzcSO099/z h0mXv3sOKNhrUSPmvtt1vtKqskn+5M6pBisTqmZ9vU7S2umE4wMrDalVq6i0jMYoRjWT BbtQ==
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:content-type; bh=GokLw8k4KTrWUHiyGkaPsEb+eTNbJwdgCbTxGIZxehE=; b=MILTIRq+ezpguthhxGROKli8I/TqiB18d+HXpF2MScBoJKxM25BDcs6rXVnVErgMdq 4IK1qY2kvcffvGpS5GQl5yDYH91T/DjJwF2u9RLGUY+S2+C5iSchzr7/SWWFieGqnX9N rgc7k5H5rN1iBFNM5BhAtAnsKBvWrqFKDF2s74OPyjxgxI11/tjwNADMTeAfJkqjzQPm a58Ik2Or4F8GfMxSG8jRiYLP9v2DHvnN/dEhV/FZwUspN7VVhmt0UbBXueyAEcMuHf6D 5uDMG1Hpp3YcnmE3wM8ouydDWq4tMp6F5NOHoRSRfuNgj5ejjx/iJ0bNib8am0fujgF5 TltA==
X-Gm-Message-State AG10YOQAFyZeV5Mm/4aKOlr9e3LAvczw33ivsiMRWyspQMwdSAb2hMYRZuEXx7ckftP7FIRwW0idOMm3r72wNw==
X-Received by 10.50.28.19 with SMTP id x19mr6422017igg.92.1453030197166; Sun, 17 Jan 2016 03:29:57 -0800 (PST)
In-Reply-To <n7fshb$f36$1@news2.informatik.uni-stuttgart.de>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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:101847

Show key headers only | View raw


On Sun, Jan 17, 2016 at 10:05 PM, Ulli Horlacher
<framstag@rus.uni-stuttgart.de> wrote:
> Chris Angelico <rosuav@gmail.com> wrote:
>> On Sun, Jan 17, 2016 at 8:51 PM, Ulli Horlacher
>> <framstag@rus.uni-stuttgart.de> wrote:
>> > I have seen at several places "x == None" and "x is None" within
>> > if-statements.
>> > What is the difference?
>> > Which term should I prefer and why?
>>
>> tl;dr: Prefer "x is None" as a check.
>
> And for the negation?
> "if not x is None" or "if x is not None"
>
> I have seen the last one several times, but i do not understand it, because:
>
>>>> x=0
>>>> x is not None
> True
>>>> not None
> True
>>>> x is True
> False

There's no actual difference:

>>> dis.dis(lambda x: x is not None)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_CONST               0 (None)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE
>>> dis.dis(lambda x: not x is None)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_CONST               0 (None)
              6 COMPARE_OP               9 (is not)
              9 RETURN_VALUE

The compiler can tell that "not x is None" is exactly the same thing
as "x is not None". Personally, I'd rather spell it "is not None",
partly because it reads more like English that way, and partly because
one operator is better than two; but you're welcome to spell it "not x
is None" if that has other benefits (eg consistency).

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

"x == None" vs "x is None" Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-01-17 09:51 +0000
  Re: "x == None" vs "x is None" Chris Angelico <rosuav@gmail.com> - 2016-01-17 21:15 +1100
    Re: "x == None" vs "x is None" Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-01-17 11:05 +0000
      Re: "x == None" vs "x is None" Peter Otten <__peter__@web.de> - 2016-01-17 12:26 +0100
      Re: "x == None" vs "x is None" Chris Angelico <rosuav@gmail.com> - 2016-01-17 22:29 +1100
      Re: "x == None" vs "x is None" <paul.hermeneutic@gmail.com> - 2016-01-17 11:01 -0700
      Re: "x == None" vs "x is None" Random832 <random832@fastmail.com> - 2016-01-17 16:33 -0500
      Re: "x == None" vs "x is None" Chris Angelico <rosuav@gmail.com> - 2016-01-18 08:38 +1100
      Re: "x == None" vs "x is None" Ben Finney <ben+python@benfinney.id.au> - 2016-01-18 13:46 +1100
  Re: "x == None" vs "x is None" fernando junior <fernandojr.ifcg@live.com> - 2016-01-19 14:20 -0800

csiph-web