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

Grant Griffin not.this at seebelow.org
Wed Jun 12 17:12:39 EDT 2002

Hi Gang,

One of the things I like about Python is that it works as expected.  However, I
sometimes accidently expect it to allow one to use "in" to determine if one
string is "in" another, e.g.:

>>> 'in' in 'in'

which yields a traceback:

   Traceback (most recent call last):
     File "<interactive input>", line 1, in ?
   TypeError: 'in <string>' requires character as left operand

Evidently one is invited to use "count" or "find" instead:

>>> 'in'.count('in') > 0
>>> 'in'.find('in') >= 0

I prefer "find" over "count" because it (presumably) punts after the first
occurrence.  However, neither one reads nearly as well as the (illegal) "in"

So why is the "in" operator limited to single characters when used as a
membership operator?  This peculiar lack of generality seems a bit (dare I say
it?) "non-Pythonic".  (That said, "Pythonic" seems to be a bit like
"enlightenment": those who claim to have mastered it wisely avoid spelling out
any concrete criteria by which others can judge their mastery <wink>.)

Perhaps its somehow due to the overloading of "in", which is used both as a
membership operator and as a separator gizmo inside "for" loops.  ("It's a floor
wax/No, it's a desert topping/No it's both!")  But is that any good reason to
exclude the handy, sensible, and downright intuitive use of "in" as a substring

Or maybe support for multiple characters would penalize the performance of the
single-character "in".

Or maybe Tim just doesn't want to burden us with a third way to do it <wink>.

   -and-reading-as-pseudo-code-ly y'rs,



Grant R. Griffin                                           g2 at dspguru.com
Publisher of dspGuru                               http://www.dspguru.com
Iowegian International Corporation                http://www.iowegian.com

More information about the Python-list mailing list