On 08/11/2021 21:43, Ethan Furman wrote:
When is an empty container contained by a non-empty container?
For example: These examples are not at all analogous. `a in b` has different meanings for different classes of b.
{} in {1:'a', 'b':2] <-- TypeError because of hashability `x in aDict` means `x in aDict.keys()` 1 in {1:'a', 'b':2} == True 'a' in {1:'a', 'b':2} == False
set() in {1, 2, 'a', 'b'} <-- ditto [] in ['a', 'b', 1, 2] <-- False `x in aSet' / `x in a List` means x is one of the members of aSet/aList. E.g. {1, 2, 'a', 'b'} has 4 members: 1, 2, 'a', 'b'. None of these equals set().
'' in 'a1b2' <-- True
`x in aStr` means x is a substring of aStr (quite different). The empty string is (vacuously but correctly) a substring of any string. This may be inconvenient sometimes (as you say) but it is the logically correct behaviour. Just as the modulus operator for ints returns the mathematically correct result (unlike in some languages such as C / C++) even though this may not always be what you want. Best wishes Rob Cliffe