<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Tue, Apr 3, 2018 at 7:34 PM Ethan Furman <<a href="mailto:ethan@stoneleaf.us">ethan@stoneleaf.us</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This behavior was recently brought to my attention [1]:<br>
<br>
--> 1 in 'hello'<br>
Traceback (most recent call last):<br>
File "<stdin>", line 1, in <module><br>
TypeError: 'in <string>' requires string as left operand, not int<br>
<br>
However, in any other collection (set, dict, list, tuple, etc), the answer would be False.<br>
<br>
Does anyone remember the reason why an exception is raised in the string instance instead of returning False?<br></blockquote><div><br></div><div>If I had to hazard a guess, I'd say it's because strings by definition, in the sense that they are a container, can only contain strings of length 1, whereas the other containers you listed can contain a wide variety of Python objects (in some cases, any Python object). Thus it's easy to detect and raise the TypeError.<br><br></div><div>Note that `[] in set()` also raises TypeError, so it's consistent: values of types that are impossible for the container to contain generally raise TypeError (in this case, an unhashable type and a container that only accepts hashable types).<br><br></div><div>(I may be oversimplifying this a bit, but I think the basic idea is right.)<br></div></div></div>