Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: "x == None" vs "x is None" Date: Sun, 17 Jan 2016 21:15:21 +1100 Lines: 41 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de WLihMPkMWROJrrvMBxHI7QwtSv0RHYSC3iEz0Kt3r83g== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'none:': 0.05; 'semantic': 0.07; 'cc:addr:python-list': 0.09; 'check.': 0.09; 'none"': 0.09; 'none.': 0.09; 'pointers': 0.09; 'subject:None': 0.09; 'jan': 0.11; 'def': 0.13; 'do,': 0.15; '"does': 0.16; '"is': 0.16; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'instance:': 0.16; "objects'": 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'singleton': 0.16; 'wrote:': 0.16; 'cheap': 0.18; 'comparing': 0.18; '(in': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'not,': 0.22; 'slightly': 0.23; 'header:In-Reply-To:1': 0.24; 'possibility': 0.27; 'question': 0.27; 'message- id:@mail.gmail.com': 0.27; 'equality': 0.29; 'itself,': 0.29; 'objects': 0.29; 'code': 0.30; 'checks': 0.30; 'says': 0.32; 'generally': 0.32; 'class': 0.33; 'equal': 0.34; 'received:google.com': 0.35; 'false': 0.35; 'identity': 0.35; 'asking': 0.35; 'comment': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'subject:" ': 0.36; 'pm,': 0.36; 'two': 0.37; 'there,': 0.37; 'received:209.85.213': 0.37; 'itself': 0.38; 'received:209': 0.38; 'several': 0.38; 'someone': 0.38; 'along': 0.39; 'some': 0.40; 'term': 0.60; 'your': 0.60; 'skip:n 10': 0.62; 'more': 0.63; 'different': 0.63; 'within': 0.64; 'places': 0.64; 'decided': 0.66; 'incredibly': 0.76; 'chrisa': 0.84; 'faster.': 0.84; 'to:none': 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=9MDp5RWXz5C4lnkmLntcAT/fDjUvOVN4g//Qm1WB3Oc=; b=f7t8HhrFN42IseErY+KqiYW1ryKKDEU0wckijGjECZ1wb+tseBKo9lbJp9gST96Y4x xlZRLmQMlKs359VMe/S3VZyIdyhFfhANFDEEeHayGCr7iZvKjOpG/+vSm1Zs49tKY9hg spY4q7qpI+6KM1hMBuHZpDeGfVMo5HGIIcmvvo3ParFiEgT6RWG9+RO+23gyMGDIZmB2 njG7mJQ0+wa6Zio9VFfDelFKsQ/vukWC+kBo0p4ozGXNTm376vNllz5PYuJ02kv9phjr L7yGFR5MHsi4bjL1rOIUlC7YCaszclRZ35VBJGwUZiEhNHOtp7AD04YO+6ZcmsW/zrza MR9A== 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=9MDp5RWXz5C4lnkmLntcAT/fDjUvOVN4g//Qm1WB3Oc=; b=Vo4YURJ2VKkkJzsAFXDO5BeuECc1DOfZS4hWN2Sj7Bq6+ZSAgHzddA3RfXrfCRGgd4 ujxUwUctJxOJqWfvbUBsfVFylAcAG0aFQhsScgZFXZSyHHb+A/mLVh7X63dYTMxUXvwC JShOGPYey1wwAdc7NP3uo9PD5FHl+j1HGz0lWhd/aVr/U8WmpU5ZaxdLzDhYTmj9ksXq 2Ysyl2c8I5w3TgbN0XIKUXwjWDGy9+hHitceV9qdqLGRPuGHV0O9Ac9vjDg8ftf93mnR P/dibvsZ5TqHc1KKjWAYG/Ru9LTBAgGQZ1mlcmQCabkz6n5KuOcTecqP0jE0+Yx/Akq4 KJ0w== X-Gm-Message-State: AG10YOQI2OGarx7KqySrgnXKH2c3W0t6Kwh07yy26bk7kAK4+jbj0abs47szQIaaTiY0Z3ab27Nr5h4qVoJ2qA== X-Received: by 10.50.66.179 with SMTP id g19mr6506761igt.94.1453025721104; Sun, 17 Jan 2016 02:15:21 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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:101839 On Sun, Jan 17, 2016 at 8:51 PM, Ulli Horlacher 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. The two checks have slightly different meaning, and almost always you want the identity check. "x is None" is asking the identity question "does the name x refer to the exact same object as the singleton None", but "x == None" is asking the value question "does the object referred to by x want to consider itself equal to None". For instance: class NoneLike(object): def __eq__(self, other): if other is None: return True return isinstance(other, NoneLike) >>> x = NoneLike() >>> x == None True >>> x is None False The object referred to as x is not None itself, but it has decided to call itself equal to None. This is incredibly unusual, and will generally not be what you want. As an additional bonus, the identity check is faster. The equality check has to ask the object if it's equal or not, but the identity check just says "same object? Y/N", which is generally going to be a cheap check (in CPython, the objects' pointers can be compared). But the main reason is semantic - you generally do not _want_ the possibility of objects comparing equal to None. And if you actually do, you'd better have some kind of comment there, or someone will come along and change your code to the more normal "is None". ChrisA