[Python-Dev] Generalizing "in" to pairs of sequences

Eric S. Raymond esr@snark.thyrsus.com
Sun, 26 Aug 2001 05:11:46 -0400

This seems to be my week to ask simple, stupid questions.

Is there any good semantic or philosophical reason that these aren't legal?

>>> "ab" in "cabcd"
>>> "xy" in "cabcd"
>>> (1, 2) in (0, 1, 2, 3)
>>> (9, 8) in (0, 1, 2, 3)

Proposed -- the following definition of "in" as a binary operation on
sequence types: a is "in" b if a occurs as a subsequence of b.

I'd like to see Python support this mainly to get rid of the following
common but ugly idiom:

if b.find(a) > -1:
    # Do one thing
    # Do another

It's really easy to forget and miscode the guard "if b.find(a):". 
By contrast, see how much clearer this is:

if a in b:
    # Do one thing
    # Do another

		<a href="http://www.tuxedo.org/~esr/">Eric S. Raymond</a>

I don't like the idea that the police department seems bent on keeping
a pool of unarmed victims available for the predations of the criminal
         -- David Mohler, 1989, on being denied a carry permit in NYC