473,480 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

__contains__() and overload of in : Bug or Feature ???

Thanks for your quick response.
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
Not sure what you're trying to achieve,
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.
but the semantics of the "in" operator
make it return a boolean value.
That is why I need to overload it.
The string "yop" evaluates to the boolean
value True, as it is not empty.
Does it means that when overloading an operator, python just
wrap the call to the method and keep control of the returned
values ??? Is there a way to bypass this wrapping ???

Sep 21 '07 #1
3 2020
"se**************@gmail.com" <se**************@gmail.comwrites:
>The string "yop" evaluates to the boolean value True, as it is not
empty.

Does it means that when overloading an operator, python just
wrap the call to the method and keep control of the returned
values ???
In case of 'in' operator, it does.
Sep 21 '07 #2
On Fri, 2007-09-21 at 13:57 +0000, se**************@gmail.com wrote:
Does it means that when overloading an operator, python just
wrap the call to the method and keep control of the returned
values ??? Is there a way to bypass this wrapping ???
The answers are "No in general, but yes in this case" and "No, not
easily."

Your problem is that the "in" operator is a comparison operator, which
by definition returns either True or False. Python is not meant to be
used as a Domain-Specific Language, so if you try to use it as one,
you'll run into limitations. The fact that comparison operators always
have boolean semantics is one of those limitations. (And yes, I imagine
this is a feature, so that callers of comparison operators don't have to
worry about checking whether the call really returned a boolean value,
they can just rely on the fact that it did.)

To bypass this behavior, I suppose you could try to change this by
modifying Python's source code directly, but who knows what you might
break in the process if you break the contract that "in" always returns
True or False.

In other words, try to find a different solution.

--
Carsten Haese
http://informixdb.sourceforge.net
Sep 21 '07 #3
se**************@gmail.com wrote:
Thanks for your quick response.
>>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
>Not sure what you're trying to achieve,

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.
>but the semantics of the "in" operator
make it return a boolean value.

That is why I need to overload it.
>The string "yop" evaluates to the boolean
value True, as it is not empty.

Does it means that when overloading an operator, python just
wrap the call to the method and keep control of the returned
values ??? Is there a way to bypass this wrapping ???
Can you not achieve what you wish to do with a conditional
expression?

"yop" if a in b else "nop"

Colin W.

Sep 23 '07 #4

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

Similar topics

1
4557
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
1680
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...
7
2312
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...
3
1526
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'...
0
6903
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
7071
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...
1
6726
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5318
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4763
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
170
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.