is this a bug?

Alex Martelli aleax at aleax.it
Sun Jul 7 17:22:40 EDT 2002


news wrote:

> Python 2.0 (#1, Feb  7 2001, 22:41:16)
> [GCC 2.95.2 19991024 (release)] on sunos5
> Type "copyright", "credits" or "license" for more information.
>>>> "aa" in ["aa", "ab"]
> 1
>>>> "aa" in ["aa"]
> 1
>>>> "aa" in ("aa")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: 'in <string>' requires character as left operand
> 
> Is this a bug, or should this be consistent between lists and tuples?
> 
> Puzzled.

It's a bug in your code.  (x) is not a tuple, whatever x -- it's
just the value of x enclosed in parentheses.  The way to write a
tuple whose only item is x is (x,) -- note the comma.

So, 'aa' in ('aa') is just line 'aa' in 'aa' -- incorrect because
the RHS of 'in' is a string and the LHS has more than one character.

'aa' in ('aa',) is correct and does return 1 (True, in more
recent versions of Python, but that's still 1 with a false moustache).


Alex




More information about the Python-list mailing list