why not "'in' in 'in'"?

Bjorn Pettersen BPettersen at NAREX.com
Thu Jun 13 13:35:09 EDT 2002


> From: Grant Griffin [mailto:not.this at seebelow.org] 
> 
> In article <mailman.1023978378.11084.python-list at python.org>, 
> "Mark says...
> >
> >I haven't followed this thread.  Someone else has probably already 
> >suggested this, but in case they haven't, it's fairly easy to define 
> >your own function that does what you want:

[snip]

> 
> That's not a bad suggestion, Mark, but if I did that I guess 
> I would be forever wondering if I was testing whether the 
> first one was in the second or the second one was in the 
> first.  Again, it doesn't read nearly as well as:
> 
>    if x in subject:
>        ...
> 
> which leaves no doubt.

Ok, how about:

>>> class mystr(str):
...   def __contains__(self, s):
...     return self.find(s) >= 0
...
>>> 'hello' in mystr('hello world')
1
>>> 'foo' in mystr('hello world')
0
>>>

-- bjorn





More information about the Python-list mailing list