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? -- ~Ethan~ [1] https://bugs.python.org/msg314900
On Tue, Apr 3, 2018 at 7:34 PM Ethan Furman
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?
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. 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). (I may be oversimplifying this a bit, but I think the basic idea is right.)
It's because when the rhs is a string, 'in' tests for a substring rather
than simple containment. E.g. "ab" in "abc" gives True. So here 'in' is not
a collection membership, it only operates on two strings.
On Tue, Apr 3, 2018 at 4:34 PM, Ethan Furman
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?
-- ~Ethan~
[1] https://bugs.python.org/msg314900 _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/guido% 40python.org
-- --Guido van Rossum (python.org/~guido)
:
On 3 April 2018 at 20:34, Ethan Furman
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?
The comparison doesn't seem to me to be valid: the 'in' operator for all of those other collections tests whether an element is a member of a collection, whereas for a string it tests whether a string is a substring of another string. In the first case, arbitrary objects can be members, but e.g. [2, 3, 4] in [1, 2, 3, 4, 5] is False. In the second case, no non-string can ever be 'in' a string, but 'bcd' in 'abcde' is True. It's kinda-sorta like addition for numerics versus sequences; they do different things. -[]z.
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!).
participants (5)
-
Ethan Furman
-
Guido van Rossum
-
Jonathan Goble
-
MRAB
-
Zero Piraeus