[Ethan Furman <et...@stoneleaf.us>]
> When is an empty container contained by a non-empty container?

That depends on how the non-empty container's type defines
__contains__. The "stringish" types (str, byte, bytearray) work _very_
differently from others (list, set, tuple) in this respect.

   t in x

for the latter answers whether t is contained in x, but in the former
case answers whether t is a _sub_sequence (contiguous) of x.

So, e.g.,

>>> "ab" in "cab"
True
>>> b"ab" in b"cab"
True
>>> bytearray(b"ab") in bytearray(b"cab")
True

while

>>> ['a', 'b'] in ['c', 'a', 'b']
False
>>> ('a', 'b') in ('c', 'a', 'b')
False

For every stringish type S, S() (the empty value of that type) is a
subsequence of every value of type S, and `in` reflects that truth.

> Personally, I have never had a use for '' in 'some string' being True, and
> always have to guard against that scenario.

Don't know whether or not any of my code relies on it, but am pretty
sure any relevant code would have already special-cased an empty
string out any code path that would have tried to apply 'in` to it.
Not particularly because it would be surprised by True, but because
there's nothing useful it could _do_ with an empty string regardless
(e.g., every slice of it would be equal to the original - an empty
string is an end case for any string algorithm).

> .. on the other hand, it seems that collections of related flags are often 
> treated
> as in set theory, where the empty set is a member of any non-empty set.

Not how any set theory I've ever seen works: a set S contains the
empty set if and only if some member of S _is_ the empty set.  Which
is also how Python's frozenset.__contains__ works (a plain Python set
can never contain the empty set, because only a frozenset can contain
a set (empty or not)).
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/SSIRKW7TR5RUTOXLFWVGI7NAIZOHQKGH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to