"in"consistency?

castironpi castironpi at gmail.com
Tue Jul 8 17:29:08 CEST 2008


On Jul 7, 5:02 pm, Gary Herron <gher... at islandtraining.com> wrote:
> Nick Dumas wrote:
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
>
> > [1,2] in [1,2,3] checks to see if the list [1,2] is an item in [1,2,3].
> > Because the list [1,2,3] only contains the integers 1,2,3, the code
> > returns a False. Try "[1,2] in [[1,2],[2,3]]"
>
> The inconsistency goes deeper than that.  For instance, the type of a
> value returned by the indexing operation:
>
>    Indexing a string returns a string (of length 1 of course),
>    while indexing a list does not (necessarily) return a list.
>
> Conclusion:  They are different types supporting different operations.  
> Given all the obvious differences (mutability, sorting and other
> methods, types of individual elements), I'd say there are more
> differences than similarities, even though, as sequences,  they both
> support a small subset of similar operations.
>
> Gary Herron
>
> > David C. Ullrich wrote:
>
> >> Luckily I tried it before saying no, that's
> >> not how "in" works:
>
> >>>>> 'ab' in 'abc'
>
> >> True
>
> >>>>> [1,2] in [1,2,3]
>
> >> False
>
> >> 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...

Strings are not containers.

Another container type:

Python 3.0b1 on win32
>>> {0} in {0,1}
False

Another string-like, non-container type:

>>> bytes( [ 0, 1 ] ) in bytes( [ 0, 1, 2 ] )
True



More information about the Python-list mailing list