__contains__() and overload of in : Bug or Feature ???
steve at holdenweb.com
Fri Sep 21 16:29:21 CEST 2007
sebastien.lannez at 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'
>>>>>> print 'toto' in a
>>>>>> print a.__contains__('toto')
>> 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.
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
More information about the Python-list