Message287271
| Author |
Eric Lafontaine |
| Recipients |
Eric Lafontaine, christian.heimes, docs@python, ezio.melotti, mark.dickinson, nparikh, r.david.murray, vstinner |
| Date |
2017年02月08日.02:20:47 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<CAAaADLKGxms-Bjriwp1F_tE+VR_u2BS_1eF6wAGHUzu4py5BBw@mail.gmail.com> |
| In-reply-to |
<1486519839.55.0.751174767562.issue16011@psf.upfronthosting.co.za> |
| Content |
(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 <report@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 <report@bugs.python.org>
> <http://bugs.python.org/issue16011>
> _______________________________________
> |
|