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

Carsten Haese carsten at uniqsys.com
Fri Sep 21 10:34:27 EDT 2007


On Fri, 2007-09-21 at 13:57 +0000, sebastien.lannez at 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





More information about the Python-list mailing list