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

S

sebastien.lannez

Thanks for your quick response.
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 ???
 
S

Steve Holden

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.


That is why I need to overload it.


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 ???
Certain aspects of the interpreter's behavior have ot be hard-wired in
order for it to accomplish anything. This is one of the hard-wried
aspects, so unless you want to change the interpreter's implementation
I'm afraid you can't change it.

regards
Steve

--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline
 
C

Carsten Haese

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.
 
C

Colin J. Williams

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.


That is why I need to overload it.


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top