On Thu, Oct 28, 2021 at 9:05 PM Steven D'Aprano <[email protected]> wrote:
>
> On Thu, Oct 28, 2021 at 05:25:52PM +1100, Chris Angelico wrote:
>
> > But the "in" operator isn't built on iteration, so that would be
> > in-consistent.
>
> "In-"consistent, heh :-)
Couldn't resist.
> >>> a = iter("abcde")
> >>> a.__contains__
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: 'str_iterator' object has no attribute '__contains__'
> >>> 'b' in a
> True
> >>> list(a)
> ['c', 'd', 'e']
>
> https://docs.python.org/3/reference/expressions.html#membership-test-operations
>
> The "in" operator is built on iteration, but can be overridden by the
> `__contains__` method.
>
Ah, my bad. Didn't realise that it has a default implementation like
that. But still, the fundamental here is that it can and often will be
overridden.
ChrisA
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/DNB23XF2UYH5CBKVBXF2HPTUMM6QRWJO/
Code of Conduct: http://python.org/psf/codeofconduct/