473,386 Members | 1,738 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,386 software developers and data experts.

__contains__() : Bug or Feature ???

Hi everybody,

I need to overload the operator in and let him
return an object ... It seems it is not a
behavior Python expect :
>>class A:
.... def __contains__(self,a):
.... return 'yop'
....
>>a=A()
print 'toto' in a
True
>>print a.__contains__('toto')
yop

I don't know if it's a bug or a feature but i
really need this overloading and don't know
how to do it ... Furthermore I can't use
a trick of the kind : a |in| b ... because
my overloaded operator must be called by : a in b ...

Is someone could help me ?

Sep 21 '07 #1
3 1523
se**************@gmail.com wrote:
I need to overload the operator in and let him
return an object ... It seems it is not a
behavior Python expect :
>>>class A:
... def __contains__(self,a):
... return 'yop'
...
>>>a=A()
print 'toto' in a
True
Not sure what you're trying to achieve, but the semantics of the "in" operator
make it return a boolean value. The string "yop" evaluates to the boolean
value True, as it is not empty.

Stefan
Sep 21 '07 #2
On Fri, 2007-09-21 at 13:08 +0000, se**************@gmail.com wrote:
Hi everybody,

I need to overload the operator in and let him
return an object
Why do you think you need to do that? What's the underlying problem
you're trying to solve?

--
Carsten Haese
http://informixdb.sourceforge.net
Sep 21 '07 #3
"se**************@gmail.com" <se**************@gmail.comwrites:
I need to overload the operator in and let him return an object
... It seems it is not a behavior Python expect :
Python expects it all right, but it intentionally converts the value
to a boolean. The 'in' operator calls PySequence_Contains, which
returns a boolean value at the C level. User-supplied __contains__ is
implemented as an adaptor in typeobject.c (slot_sq_contains). It
takes the value returned by your __contains__ implementation and
converts it to 0 or 1.

I don't think you can overload 'in' as you want without pervasive
changes to CPython source code.
Sep 21 '07 #4

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...
2
by: Anand S Bisen | last post by:
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...
18
by: Kamen Yotov | last post by:
hi all, i first posted this on http://msdn.microsoft.com/vcsharp/team/language/ask/default.aspx (ask a c# language designer) a couple of days ago, but no response so far... therefore, i am...
30
by: Raymond Hettinger | last post by:
Proposal -------- I am gathering data to evaluate a request for an alternate version of itertools.izip() with a None fill-in feature like that for the built-in map() function: >>> map(None,...
12
by: Raymond Hettinger | last post by:
I am evaluating a request for an alternate version of itertools.izip() that has a None fill-in feature like the built-in map function: >>> map(None, 'abc', '12345') # demonstrate map's None...
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...
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':...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.