473,605 Members | 2,590 Online
Bytes | Software Development & Data Engineering Community
+ 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__(se lf,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 2028
"se************ **@gmail.com" <se************ **@gmail.comwri tes:
>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__(se lf,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
4572
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 distinct methods: def has_key(self, key): return self.data.has_key(key) def __contains__(self, key): return key in self.data
2
1691
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 __contains__ method and it fails on python 2.2 For example (Python 2.3)
7
2321
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 __contains__ still works as before after overriding __getitem__. So even though instance does not raise KeyError, I still get (as desired) 'key' in instance == False. Looking forward:
3
1534
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' .... True
0
8004
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8425
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8288
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6743
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5886
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1271
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.