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

Erik Max Francis max at alcyone.com
Thu Jun 13 21:45:25 CEST 2002


Grant Griffin wrote:

> Again, it has to do with the fact that the illegal multi-character
> "in"
> is intuitive and reads better.  Compare the following:
> 
>         if 'mortgage' in email_subject:
>            ...
> 
> to:
> 
>         if email_subject.find('mortgage') >= 0:
>            ...
> 
> I consider the former to be highly practical, if not highly pure.

The problem here is that you're declaring it more intuitive because
you've already decided a priori that you want it.  To a true Python
programming, the line

	if 'mortgage' in email_subject:

is pretty suspicious; it makes it sound like email_subject is a sequence
of keywords.

> As to the latter, I frequently made this mistake at first:
> 
>         if email_subject.find('mortage'):
>            ...
> 
> which is fairly intutitive, but doesn't work, of course, because
> "find"
> returns -1 (logical true) in the failure case!

That is just the nature of string functions; it is documented well.  Not
every aspect of Python programming can be short _and_ do what you
expect.  Some learning is required.  That learning is what tells you the
`in' operator is for membership, not subsequence comparison.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Who'd ever think it / Such a squalid little ending
\__/ The American and Florence, _Chess_
    Church / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.



More information about the Python-list mailing list