None in string => TypeError?

Chris Angelico rosuav at gmail.com
Mon Jun 9 12:31:54 EDT 2014


On Tue, Jun 10, 2014 at 2:14 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
>> This is very Pythonic, Python is strictly typed language. There's no way
>> None could possibly be "inside" a string,
>
> Then `None in some_string` could immediately return False, instead of
> raising an exception.

Note, by the way, that CPython does have some optimizations that
immediately return False. If you ask if a 16-bit string is in an 8-bit
string, eg "\u1234" in "asdf", it knows instantly that it cannot
possibly be, and it just returns false. The "None in string" check is
different, and deliberately so.

I do prefer the thrown error. Some things make absolutely no sense,
and even if it's technically valid to say "No, the integer 61 is not
in the string 'asdf'", it's likely to be helpful to someone who thinks
that characters and integers are equivalent. You'll get an exception
immediately, instead of trying to figure out why it's returning False.

ChrisA



More information about the Python-list mailing list