__contains__() : Bug or Feature ???

Stefan Behnel stefan.behnel-n05pAM at web.de
Fri Sep 21 09:12:21 EDT 2007


sebastien.lannez at gmail.com wrote:
> 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

Not sure what you're trying to achieve, but the semantics of the "in" operator
make it return a boolean value. The string "yop" evaluates to the boolean
value True, as it is not empty.

Stefan



More information about the Python-list mailing list