'<char> in <string>' works, why doesnt '<string> in <string>'?
Tim Roberts
timr at probo.com
Sun Mar 10 02:15:37 EST 2002
morton at dennisinter.com (damien morton) wrote:
>Ok, I see how this works.
>
>'<string1> in <string2>' would always returns false; as the elements
>of a <string2> are all characters you can never find <string1> amongst
>them (unless its a string of length 1).
>
>Its counter-intuitive for someone playing with strings as strings
>rather than as sequences of characters, however.
Many things that are "intuitive" are also wrong.
>'c' in 'the quick brown fox' -> 1
>'fox' in 'the quick brown fox' -> 1 (instead of TypeError)
>
>Is there any circumstance where youd expect (or want) the later not to
>work? I expected and wanted it to work (but instead got an error), its
>quite a clear and unambiguous expression (for strings only).
It would be a clear and unambiguous expression _if_ the "in" operator was a
substring search operator, but it is not. It is a set membership operator.
It works EXACTLY the same way on all sequence types: the right-hand operand
is a sequence, and the left-hand operand is something that can be an
element of that sequence type. You are suggesting an unorthogonal change
just because it is easier to type than:
'the quick brown fox'.find('fox')
No. That is NOT a good reason to change a language in an inconsistent way.
--
- Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list
mailing list