__contains__ inconsistencies between Python 2.2 and 2.3

Steven Bethard steven.bethard at gmail.com
Fri Mar 4 15:28:49 EST 2005


Anand S Bisen wrote:
> For example
> 
> (Python 2.3)
>  >> x="Hello World"
>  >> print x.__contains__("Hello")
> True
> 
> (Python 2.2)
> 
>  >>> x="Hello world"
>  >>> print x.__contains__("Hello")
> 
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
> TypeError: 'in <string>' requires character as left operand
> 
> Is there any woraround for this or what am i doing wrong in 2.2 ?

IIRC, until Python 2.3, __contains__ only accepted strings of length 1. 
  Before Python 2.3, I think you needed to do something like:

py> x = 'Hello world'
py> x.find('Hello') != -1
True
py> x.find('wrld') != -1
False

STeVe



More information about the Python-list mailing list