Terry Reedy tjreedy at udel.edu
Tue Jul 8 03:22:04 CEST 2008

David C. Ullrich wrote:

>>>> 'ab' in 'abc'
> True

'a' in 'abc' works according to the standard meaning of o in collection.

'ab' in 'abc' could not work by that standard meaning because strings, 
as virtual sequences, only contain characters (length 1 strings).  Among 
built-in collections, this limitation is unique to strings (and bytes, 
in 3.0).  So in 2.3, 'in' was given a useful extension of meaning that 
is also unique to strings (and bytes).

>>>> [1,2] in [1,2,3]
> False

[1,2] can be an member of tuples, lists, dicts and other general 
collections.  [1,2] in collection therefore has that meaning, that it is 
a single element of collection.  Extending the meaning would conflict 
with this basic meaning.

> Is there a reason for the inconsistency? I would
> have thought "in" would check for elements of a
> sequence, regardless of what sort of sequence it was...

It is not an inconsistency but an extension corresponding to the 
limitation of what an string element can be.

Terry J. Reedy

More information about the Python-list mailing list