[Python-Dev] in (original) (raw)

Guido van Rossum guido at python.org
Tue Apr 3 19:47:45 EDT 2018


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 <ethan at stoneleaf.us> wrote:

This behavior was recently brought to my attention [1]:

--> 1 in 'hello' Traceback (most recent call last): File "", line 1, in TypeError: 'in ' 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 at 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) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20180403/713ba1f1/attachment-0001.html>



More information about the Python-Dev mailing list