why not "'in' in 'in'"?
Grant Griffin
not.this at seebelow.org
Thu Jun 13 01:15:14 EDT 2002
Erik Max Francis wrote:
>
> Grant Griffin wrote:
>
> > 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".
>
> On the contrary, it is exquisitely Pythonic.
(I sure hope my "enlightenment" light will go on one day. Until then, I
guess I'll just have to bask in the warm glow of others <wink>.)
> You used the magic word
> yourself: _membership_.
In that case, perhaps I should have thought of a better one <wink>. In
any event, in the case of strings, I think one might stretch the concept
of "membership" to include substrings. In fact, note that since Python
has no actual character type (because a character is adequately
represented as a string of length one) the "in" operator actually _does_
test membership of strings in strings. But for some reason, it's
limited to testing strings of length one. Go figure!
> A string is a sequence of characters, so when
> testing membership with the `in' operator, the left-hand side of the
> expression should be a character (really a string of length one). `in'
> tests membership, not for the existence of subsequences. Would you have
> expected
>
> [1, 2] in [1, 2, 3, 4]
>
> to evaluate to true?
Honest answer: although I can't see much use for that construct, I can't
see much harm in it either. So although it's probably not worth adding
to Python, I'm sure _somebody_ would find a use for it.
But anyway, let's "just say no" to the tuple case rather than confuse
ourselves; let's just make the string version of "in" a special case.
Then, we can rest easy knowing that:
7. Readability counts.
8. Special cases aren't special enough to break the rules.
9. Although practicality beats purity.
(Emphasis on the last one.)
see-what-i-mean-about-"concrete"-principles?-<wink>-ly y'rs,
=g2
--
_____________________________________________________________________
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