473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

__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

Jul 18 '05 #1
2 1678
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
Jul 18 '05 #2
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/
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Skip Montanaro | last post by:
I got to wondering if there is a difference between __contains__() and has_key() for dictionaries (I don't think there is), so I peeked at UserDict.UserDict and saw they were implemented as...
10
by: Roger Withnell | last post by:
I seem to spend far too much of my time struggling with browser inconsistencies concerning Javascript (not to mention CSS). What do you think is the most efficient development regime, including...
7
by: David Isaac | last post by:
I have a subclass of dict where __getitem__ returns None rather than raising KeyError for missing keys. (The why of that is not important for this question.) I was delighted to find that...
5
by: rconradharris | last post by:
A co-worker of mine came across some interesting behavior in the Python interpreter today and I'm hoping someone more knowledgeable in Python internals can explain this to me. First, we create...
8
by: Darren Dale | last post by:
I was just searching for some guidance on how to name packages and modules, and discovered some inconsistencies on the www.python.org. http://www.python.org/doc/essays/styleguide.html says "Module...
2
by: John Hanley | last post by:
I am getting some inconsistencies with mktime(). I allocate memory for my struct tm early in my program, and assign only *some* of the member variables. t->tm_sec=s; t->tm_min=m;...
3
by: sebastien.lannez | last post by:
Hi everybody, I need to overload the operator in and let him return an object ... It seems it is not a behavior Python expect : .... def __contains__(self,a): .... return 'yop'...
3
by: sebastien.lannez | last post by:
Thanks for your quick response. Using Python as an algebraic parser for symbolic mathematical equation and I need that the 'in' operator returns an object based on its two arguments.
2
by: eboy98 | last post by:
if i have a dictionary name number ....and i want to ask the list whether a particular key already exists. {'octal': '1234567', 'binary': '10100101', 'decimal': '1234567890', 'hexadecimal':...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.