[Python-Dev] string.find() again (was Re: timsort for jython)

Guido van Rossum guido@python.org
Mon, 05 Aug 2002 13:56:42 -0400


> I want to raise one other issue here:  should
> 
>     '' in 'xyz'
> 
> return True or raise an exception?  I've been burned, e.g., by
> 
>     >>> 'xyz'.startswith('')
>     True
>     >>>
> 
> when '' was computed by an expression that didn't "expect to" reduce to
> nothingness, and I expect *everyone* here has been saved more than once by
> that
> 
>     '' in 'xyz'
> 
> currently raises an exception.

I dunno.  The exception has annoyed me too.

> If we make __contains__ act like
> 
>     'xyz'.find('') >= 0
> 
> that (very probable) error will pass silently in the future:
> 
>     >>> 'xyz'.find('')
>     0
>     >>>
> 
> IOW, do we follow find() rigidly, or retain "str1 in str2"'s current
> behavior when str1 is empty?

I expect that Andrew Koenig would delight in this question. :-)

I personally see no way to defend ('' in 'x') returning false; it's so
clearly a substring that any definition of substring-ness that
excludes this seems mathematically wrong, despite your good
intentions.

I guess we'll have to cope in the same way as we cope with the
behavior of find() and startswith() in similar cases.

--Guido van Rossum (home page: http://www.python.org/~guido/)