[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"
1
>>> "xy" in "cabcd"
0
>>> (1, 2) in (0, 1, 2, 3)
1
>>> (9, 8) in (0, 1, 2, 3)
0

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
else:
    # 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
else:
    # Do another

Thoughts?
-- 
		<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
class.
         -- David Mohler, 1989, on being denied a carry permit in NYC