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


Groups > comp.lang.python > #73043

Re: None in string => TypeError?

Date 2014-06-09 18:57 +0300
From Paul Sokolovsky <pmiscml@gmail.com>
Subject Re: None in string => TypeError?
References <048960da-c132-407f-b1b3-4612a3dd7697@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.10922.1402329457.18130.python-list@python.org> (permalink)

Show all headers | View raw


Hello,

On Mon, 9 Jun 2014 08:34:42 -0700 (PDT)
Roy Smith <roy@panix.com> wrote:

> We noticed recently that:
> 
> >>> None in 'foo'
> 
> raises (at least in Python 2.7)
> 
> TypeError: 'in <string>' 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

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


Thread

None in string => TypeError? Roy Smith <roy@panix.com> - 2014-06-09 08:34 -0700
  Re: None in string => TypeError? Ryan Hiebert <ryan@ryanhiebert.com> - 2014-06-09 10:42 -0500
  Re: None in string => TypeError? Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-09 09:50 -0600
  Re: None in string => TypeError? Paul Sokolovsky <pmiscml@gmail.com> - 2014-06-09 18:57 +0300
    Re: None in string => TypeError? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-06-09 16:14 +0000
      Re: None in string => TypeError? Chris Angelico <rosuav@gmail.com> - 2014-06-10 02:31 +1000
  Re: None in string => TypeError? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-06-09 16:07 +0000
  Re: None in string => TypeError? MRAB <python@mrabarnett.plus.com> - 2014-06-09 17:06 +0100
  Re: None in string => TypeError? Shiyao Ma <i@introo.me> - 2014-06-10 00:13 +0800
  Re: None in string => TypeError? Roy Smith <roy@panix.com> - 2014-06-09 12:53 -0400
  Re: None in string => TypeError? Chris Angelico <rosuav@gmail.com> - 2014-06-10 02:59 +1000
  Re: None in string => TypeError? Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-09 11:22 -0600
  Re: None in string => TypeError? Chris Angelico <rosuav@gmail.com> - 2014-06-10 03:40 +1000
  Re: None in string => TypeError? Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-09 11:58 -0600
  Re: None in string => TypeError? Chris Angelico <rosuav@gmail.com> - 2014-06-10 04:02 +1000

csiph-web