Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

__contains__ inconsistencies between Python 2.2 and 2.3

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


vmlinuz at abisen

Mar 4, 2005, 12:19 PM

Post #1 of 3 (482 views)
Permalink
__contains__ inconsistencies between Python 2.2 and 2.3

Hello

I have been developing a code that works pretty well on my python 2.3
and now when i am running it on my server where it is programmed to run
it's giving me errors. I have been using __contains__ method and it
fails on python 2.2

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 ?

Thanks

ASB

--
http://mail.python.org/mailman/listinfo/python-list


steven.bethard at gmail

Mar 4, 2005, 12:28 PM

Post #2 of 3 (442 views)
Permalink
Re: __contains__ inconsistencies between Python 2.2 and 2.3 [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


steve at holdenweb

Mar 4, 2005, 12:32 PM

Post #3 of 3 (444 views)
Permalink
Re: __contains__ inconsistencies between Python 2.2 and 2.3 [In reply to]

Anand S Bisen wrote:
> Hello
>
> I have been developing a code that works pretty well on my python 2.3
> and now when i am running it on my server where it is programmed to run
> it's giving me errors. I have been using __contains__ method and it
> fails on python 2.2
>
> 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 ?
>
> Thanks
>
Any use of double-underscores is an indication that magic is at work. In
this case the __contains__ method is intended to be called by the
interpreter when you write

x in s

The __contains__ method was extended for strings in 2.3 so that
construct could be used as a test to see whether s contained x as a
substring. Before that, as the error message explains, it will only test
to see whether a single character is contained in the string (by analogy
with

1 in [3, 4, 5, 2]

in case you are interested).

So you'll need to use the .find() string method and say

if x.find("Hello") != -1:
... you found "Hello"

because your ISP appears to be using an older version of Python than you.

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.