Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '(at': 0.04; 'string': 0.09; 'false,': 0.09; 'raises': 0.09; 'subject:None': 0.09; 'subject:string': 0.09; 'python': 0.11; 'bug': 0.12; 'assume': 0.14; 'comparison.': 0.16; 'iterable:': 0.16; 'roy': 0.16; 'string:': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'seems': 0.21; 'instead.': 0.24; 'mon,': 0.24; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'that.': 0.31; '>>>>': 0.31; 'comparison': 0.31; 'guess': 0.33; 'noticed': 0.34; 'could': 0.34; "can't": 0.35; 'equal': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'more': 0.64; 'smith': 0.68; "'foo'": 0.84; 'noise': 0.84 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 :content-type; bh=vjm58qs+FzlybEP0XQ4vGi9sQBXkccs+TQYkk2xXid0=; b=gYSk5TyBmBL381Pib/CaTVsNtbn+gDoqjxOEY1i7tU149twE1zWX5ytE+U2CPSW6SS 2mDV5oLLiXyLny2c0PwuTLZdAA9rVWgmp1l272U/m1MgRHwLzJ3yq53tya6sz5fwzofX JwEm3V86N20GMdFSpi8xeHbcz8q8JG5Xbn877vuj2SFkYTSRNXsWrEIVS1nHmNRAbvmL arxJNdWdye9w9e5l1bohkCBNQZPHDlwt39VA2ROflSTxkuh6ScFNljB7QW2k7kprMgM2 lYIzTu63zSLPdkHIUHjB7vH7c2NHs7KNUQRaUL1DEAtZeChozdfD+KfcWdY3OoJ4PK+F Fr5g== X-Received: by 10.236.42.52 with SMTP id i40mr3039004yhb.119.1402329046073; Mon, 09 Jun 2014 08:50:46 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <048960da-c132-407f-b1b3-4612a3dd7697@googlegroups.com> References: <048960da-c132-407f-b1b3-4612a3dd7697@googlegroups.com> From: Ian Kelly Date: Mon, 9 Jun 2014 09:50:06 -0600 Subject: Re: None in string => TypeError? To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1402329049 news.xs4all.nl 2842 [2001:888:2000:d::a6]:38789 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73042 On Mon, Jun 9, 2014 at 9:34 AM, Roy Smith wrote: > We noticed recently that: > >>>> None in 'foo' > > raises (at least in Python 2.7) > > TypeError: 'in ' requires string as left operand, not NoneType > > This is surprising. The description of the 'in' operatator is, 'True if an item of s is equal to x, else False '. From that, I would assume it behaves as if it were written: > > for item in iterable: > if item == x: > return True > else: > return False > > why the extra type check for str.__contains__()? That seems very unpythonic. Duck typing, and all that. I guess for the same reason that you get a TypeError if you test whether the number 4 is in a string: it can't ever be, so it's a nonsensical comparison. It could return False, but the comparison is more likely to be symptomatic of a bug in the code than intentional, so it makes some noise instead.