Eric Lafontaine added the comment: (first time trying to reply through email)
thanks for the example and you are right : class Foo_emptylist(object): def __contains__(self,item): return [] class Foo_emptydict(object): def __contains__(self,item): return {} class Foo_emptystring(object): def __contains__(self,item): return '' for foo in [Foo_false(),Foo_None(),Foo_emptylist(),Foo_emptydict(),Foo_emptystring(),Foo_neg(),Foo_true(),Foo_42()]: print("3 in foo:" + str(3 in foo)) print("foo.__contains__(3)" + str(foo.__contains__(3))) 3 in foo:False foo.__contains__(3)False 3 in foo:False foo.__contains__(3)None 3 in foo:False foo.__contains__(3)[] 3 in foo:False foo.__contains__(3){} 3 in foo:False foo.__contains__(3) 3 in foo:True foo.__contains__(3)-42 3 in foo:True foo.__contains__(3)True 3 in foo:True foo.__contains__(3)42 So the proposition should be this then? For user-defined classes which define a __contains__() method, the in operator will apply bool() on the __contains__() method. In other words, "x in y" is equivalent to "bool(y.__contains__(x))" and will return False if bool(y.__contains__(x)) is equivalent to false. Éric Lafontaine | Membre du Projet VUE, Groupe Contrôle Génie électrique, 54ème promotion UdeS | Étudiant en maitrise TI à l'ETS VAS OPS chez Bell Mobility « Nous voulons proposer une alternative de transport en présentant un véhicule électrique spécifiquement conçu pour les déplacements urbains. » 2017-02-07 21:10 GMT-05:00 R. David Murray <rep...@bugs.python.org>: > > R. David Murray added the comment: > > >>> bool(()) > False > >>> bool([]) > False > >>> bool('') > False > > What you want to say is that 'in' coerces the result returned by > __contains__ to a boolean value. > > ---------- > > _______________________________________ > Python tracker <rep...@bugs.python.org> > <http://bugs.python.org/issue16011> > _______________________________________ > ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue16011> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com