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

Daniel Fackrell dfackrell at DELETETHIS.linuxmail.org
Thu Jun 13 19:07:03 CEST 2002

When all else fails, lets try throwing in some newbie exploration:

Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> print type("a")
<type 'str'>
>>> print type("ab")
<type 'str'>
>>> print type([1])
<type 'list'>
>>> print type([1,2])
<type 'list'>
>>> print type(1)
<type 'int'>
>>> if "a" in "ab":
    print "yes"
    print "no"

>>> if [1] in [1, 2]:
    print "yes"
    print "no"

>>> if 1 in [1, 2]:
    print "yes"
    print "no"


I can see the rationale here, as there isn't really a "character" type in
Python.  So what the "in" operator does in the case of a string as the
second operand is check the first operand to see if it can be interpreted as
a element of a string, which would be a character.

It does in fact appear to be inconsistent, but I'll have to leave it up to
people who know more about what they are doing than myself to figure out
what to do about it, if anything.  I'm not sure that the ideas that come to
mind (creating an actual "character" type or modifying the behavior as
proposed since we really have an exception here already) are any good.

Daniel Fackrell (dfackrell at linuxmail.org)
When we attempt the impossible, we can experience true growth.

"Grant Griffin" <not.this at seebelow.org> wrote in message
news:aeadtt06rq at drn.newsguy.com...
> That's not a bad suggestion, Mark, but if I did that I guess I would be
> wondering if I was testing whether the first one was in the second or the
> one was in the first.  Again, it doesn't read nearly as well as:
>    if x in subject:
>        ...
> which leaves no doubt.
> Ironically, that's already perfectly legal--so long as x has no more than
> character:
> >>> subject = "i like spam, i do."
> >>> x = "s"
> >>> y = "h"
> >>> if x in subject:
> ...    print "x is in subject and x has no more than one character"
> ...
> x is in subject and x has no more than one character
> >>> if y in subject:
> ...    print "same goes for y"
> ... else:
> ...    print "can't say the same for y"
> ...
> can't say the same for y
> >>>
> reluctant-to-discard-my-hard-won-near-mastery-of-"find"-ly y'rs,

More information about the Python-list mailing list