Message271145
Consider the attached example program. I expect it to run successfully, because the Python 3 language reference says [1]:
> For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression `x in y` is equivalent to `any(x is e or x == e for e in y)`.
and [2]:
> `x==y` calls `x.__eq__(y)`
Instead, under Python 3.5.2, the program crashes with an assertion error, because `dict.__contains__` calls `Bar.__eq__` instead of `Foo.__eq__`.
The same happens if you replace the dict with a set or a frozenset. But if you replace it with a list or a tuple, the behavior is as documented.
This seems to me like a bug in either the implementation or the documentation.
The language reference clearly says [3] that equality should be symmetric "if possible", but is not required to be, and indeed that is hard to guarantee when different classes are involved.
[1] https://docs.python.org/3/reference/expressions.html#membership-test-details
[2] https://docs.python.org/3/reference/datamodel.html#object.__eq__
[3] https://docs.python.org/3/reference/expressions.html#value-comparisons |
|
Date |
User |
Action |
Args |
2016-07-24 12:13:15 | vfaronov | set | recipients:
+ vfaronov |
2016-07-24 12:13:15 | vfaronov | set | messageid: <1469362395.06.0.246923750082.issue27605@psf.upfronthosting.co.za> |
2016-07-24 12:13:15 | vfaronov | link | issue27605 messages |
2016-07-24 12:13:14 | vfaronov | create | |
|