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

holger krekel pyth at devel.trillke.net
Thu Jun 13 14:58:52 EDT 2002


Bjorn Pettersen wrote:
> > From: Grant Griffin [mailto:not.this at seebelow.org] 
> > 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
> >>>

that's fine. but usually you don't want to make up an 
instance for every string you get from somewhere. 
I still think that patching 'stringobject.c' to get the
reasonable behaviour might be worth a try for python2.3. 

    holger





More information about the Python-list mailing list