Re: __contains__() and overload of in : Bug or Feature ???
[EMAIL PROTECTED] 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: __contains__() and overload of in : Bug or Feature ???
On Fri, 2007-09-21 at 13:57 +, [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: __contains__() and overload of in : Bug or Feature ???
[EMAIL PROTECTED] 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 ??? > 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: __contains__() and overload of in : Bug or Feature ???
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: >> 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. -- http://mail.python.org/mailman/listinfo/python-list
__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 ??? -- http://mail.python.org/mailman/listinfo/python-list