By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,473 Members | 3,598 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,473 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.