None in string => TypeError?
MRAB
python at mrabarnett.plus.com
Mon Jun 9 12:06:01 EDT 2014
On 2014-06-09 16:34, Roy Smith wrote:
> We noticed recently that:
>
>>>> None in 'foo'
>
> raises (at least in Python 2.7)
>
> TypeError: 'in <string>' requires string as left operand, not NoneType
>
> This is surprising. The description of the 'in' operatator is, 'True if an item of s is equal to x, else False '. From that, I would assume it behaves as if it were written:
>
> for item in iterable:
> if item == x:
> return True
> else:
> return False
>
> why the extra type check for str.__contains__()? That seems very unpythonic. Duck typing, and all that.
>
When working with strings, it's not entirely the same. For example:
>>> 'oo' in 'foo'
True
If you iterated over the string, it would return False.
More information about the Python-list
mailing list