[Python-Dev] string.find() again (was Re: timsort for jython)

Guido van Rossum guido@python.org
Wed, 14 Aug 2002 17:12:28 -0400

> Or unless the notion of ``x in y'' could be were reinterpreted
> in terms of a new attribute that strings, chars, and regexps
> would share.
> That is, I can imagine defining ``x in y'' anologously to ``x+y''
> as follows:
>    If x has an attribute __in__, then ``x in y'' means ``x.__in__(y)''
>    Otherwise, if y has an attribute __rin__, then ``x in y'' means
>    ``y.__rin__(x)''
> and so on.
> This is an example of the kind of situation where I imagine type
> categories would be useful.

It is already done this way, except the attribute is called
__contains__ and we only ask the right argument for it: "x in y" calls
"y.__contains__(x)" [if it exists; otherwise there's a fallback that
loops over y's items comparing them to x].

I suppose we could add __rcontains__ that was tried next, analogously
to __add__ and __radd__; or maybe it could be called __in__
instead. :-)

Unfortunately that would be a significant change in internal shit.
I'm not convinced that this particular example is worth that
(especially since chars are already taken care of -- they're just
1-char strings).

--Guido van Rossum (home page: http://www.python.org/~guido/)