Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'operator': 0.03; '(at': 0.04; 'string.': 0.05; 'element': 0.07; 'string': 0.09; 'arguments': 0.09; 'raises': 0.09; 'sanity': 0.09; 'subject:None': 0.09; 'subject:string': 0.09; 'typed': 0.09; 'wrong,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'assume': 0.14; 'suggest': 0.14; 'language.': 0.14; '"in"': 0.16; 'check",': 0.16; 'iterable:': 0.16; 'roy': 0.16; 'substring': 0.16; 'typeerror:': 0.16; 'so.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'seems': 0.21; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'string,': 0.24; 'mon,': 0.24; 'paul': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'possibly': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'start,': 0.30; 'that.': 0.31; 'another': 0.32; 'noticed': 0.34; 'there,': 0.34; 'could': 0.34; 'something': 0.35; 'equal': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'doing': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'should': 0.36; 'checks': 0.38; 'that,': 0.38; 'strictly': 0.61; "you're": 0.61; 'email addr:gmail.com': 0.63; 'great': 0.65; 'within': 0.65; 'smith': 0.68; "'foo'": 0.84; '(pdt)': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=4OoyAEvm6UZwpl0oRisZ+uwSSeltpdHLcdpA5pVBHLE=; b=DHm8QH9M3axt6BKqkLkrzwcuos7h8wh8lGdbTAVxEufncwZZ3imR9CBciqKwsAF0R0 RMIeMbr8VPqf9rxWyju0iEjYprxQztzk3uLYC+hmvKg7msq12l3ckMTe0p5BIqjxom05 1rZ8XzZi1dG+ikn+HRQP7h7VAz6x+1r4tEgSZ0we05h50prUdpF1WzAEIGRkC2xTfLEL 5ZkIyLDsIosRh1/+L0sjLygAJjQ5QIfPn1/PoADPu01T37fAfaV+1ME/UA2CVwLLoyVI lCd8ECG7rIjirs9n4tql2Bk3AYfoDuNBI9/Z0D81SqvUFOOo2/MPJE/1heM9Tq0YFgAi lt3A== X-Received: by 10.14.5.71 with SMTP id 47mr70796eek.101.1402329450840; Mon, 09 Jun 2014 08:57:30 -0700 (PDT) Date: Mon, 9 Jun 2014 18:57:28 +0300 From: Paul Sokolovsky To: Roy Smith Subject: Re: None in string => TypeError? In-Reply-To: <048960da-c132-407f-b1b3-4612a3dd7697@googlegroups.com> References: <048960da-c132-407f-b1b3-4612a3dd7697@googlegroups.com> X-Mailer: Claws Mail 3.10.0 (GTK+ 2.24.10; i686-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: python-list@python.org 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1402329457 news.xs4all.nl 2863 [2001:888:2000:d::a6]:43214 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73043 Hello, On Mon, 9 Jun 2014 08:34:42 -0700 (PDT) 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. -- This is very Pythonic, Python is strictly typed language. There's no way None could possibly be "inside" a string, so if you're trying to look for it there, you're doing something wrong, and told so. Also, it's not "extra check", it's "extra checks less", just consider that "in" operator just checks types of its arguments for sanity once at the start, and then just looks for a substring within string. You suggest that it should check for each element type in a loop, which is great waste, as once again, nothing but a string can be inside another string. -- Best regards, Paul mailto:pmiscml@gmail.com