[Python-Dev] <int> in <str>

MRAB python at mrabarnett.plus.com
Tue Apr 3 20:12:14 EDT 2018


On 2018-04-04 00:34, Ethan Furman wrote:
> This behavior was recently brought to my attention [1]:
> 
> --> 1 in 'hello'
> Traceback (most recent call last):
>     File "<stdin>", line 1, in <module>
> TypeError: 'in <string>' requires string as left operand, not int
> 
> However, in any other collection (set, dict, list, tuple, etc), the answer would be False.
> 
> Does anyone remember the reason why an exception is raised in the string instance instead of returning False?
> 
Well, strings aren't really a collection like set, etc, which can 
contain various types, even a mixture of types.
A string can contain only strings (including codepoints).

A bytestring can contain only bytestrings and ints (and there's been 
debate on whether the latter was a good idea!).


More information about the Python-Dev mailing list