__contains__ inconsistencies between Python 2.2 and 2.3

Discussion in 'Python' started by Anand S Bisen, Mar 4, 2005.

  1. 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
     
    Anand S Bisen, Mar 4, 2005
    #1
    1. Advertising

  2. 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
     
    Steven Bethard, Mar 4, 2005
    #2
    1. Advertising

  3. Anand S Bisen

    Steve Holden Guest

    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/
     
    Steve Holden, Mar 4, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skip Montanaro
    Replies:
    1
    Views:
    347
    Alex Martelli
    Oct 20, 2003
  2. Replies:
    5
    Views:
    298
    Gabriel Genellina
    Apr 12, 2007
  3. Replies:
    4
    Views:
    448
    Colin J. Williams
    Sep 24, 2007
  4. rantingrick
    Replies:
    20
    Views:
    512
    Terry Reedy
    Jul 23, 2011
  5. rantingrick
    Replies:
    0
    Views:
    193
    rantingrick
    Jul 22, 2011
Loading...

Share This Page