"in"consistency?

Terry Reedy tjreedy at udel.edu
Thu Jul 10 08:21:08 CEST 2008



castironpi wrote:
> On Jul 8, 2:25 pm, Terry Reedy <tjre... at udel.edu> wrote:

> Compare to an imaginary "set of ints" data type:
> 
>>>> a= setofints( [ 0, 1, 2 ] )
> 
> Then, the semantics of
> 
>>>> b= setofints( [ 0, 1 ] )
>>>> b in a
> True
> 
> are consistent and predictable.  Correct me if I'm wrong.

If you defined a subclass setofints of set with members restricted to 
being integers, so that [0,1] could not be a member, then you could 
(sensibly, in my opinion) override __contains__ to make the above work.

On the other hand, 'b in a' as a synonym for 'b is a subset of a' is 
already available for general sets as b.issubset(a).  So I would not do 
the work of subclassing just to abbreviate this call, but only if I 
wanted the membership guard.

Also, the transition from 'int in setofints' to 'setofints in setofints' 
is not as smooth as the transition from 'length-1 string in string' to 
'length-n string in string' because of the need to test and switch on 
whether the object tested is an int or setofints.

Tuples and lists do not have a subsequence find or test method. 
However, if I wanted one (and I expect I will sometime), I would 
probably write a general function for sequence in sequence, without 
bothering with restricting sequence membership.

Terry Jan Reedy




More information about the Python-list mailing list