<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2014-06-09 23:34 GMT+08:00 Roy Smith <span dir="ltr"><<a href="mailto:roy@panix.com" target="_blank">roy@panix.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
We noticed recently that:<br>
<br>
>>> None in 'foo'<br>
<br>
raises (at least in Python 2.7)<br>
<br>
TypeError: 'in <string>' requires string as left operand, not NoneType<br>
<br>
This is surprising.  The description of the 'in' operatator is, 'True if an item of s is equal to x, else False '.  From that, I would assume it behaves as if it were written:<br>
<br>
for item in iterable:<br>
    if item == x:<br>
        return True<br>
else:<br>
    return False<br>
<br>
why the extra type check for str.__contains__()?  That seems very unpythonic.  Duck typing, and all that.<span class=""><font color="#888888"><br></font></span></blockquote><div><br></div><div>It's a little bit inconsistent.  But it's clearly documented here: <a href="https://docs.python.org/3/reference/expressions.html#in">https://docs.python.org/3/reference/expressions.html#in</a><br>
<br></div><div>Which, according to its own logic, the string is not  a *container* type. It's just some chars, and that totally makes sense for to restrict the type of x in "str" to be convertible to type str. On the other hand, containers like list, and tuple, they are heterogeneous by default in Python, so a item by item comparison is needed.<br>
</div></div><br><br clear="all"><br>-- <br><div dir="ltr"><div><br></div><div>$B8cGZ$OG-$G$"$k!#%[!<%`!<%Z!<%8$O(B<a href="http://introo.me" target="_blank">http://introo.me</a>$B!#(B<br></div></div>
</div></div>