[Python-Dev] Getting values stored inside sets

Steven D'Aprano steve at pearwood.info
Fri Apr 3 17:41:25 CEST 2009


On Sat, 4 Apr 2009 02:07:28 am Antoine Pitrou wrote:

> Your example is wrong:

Of course it is. The perils of posting at 2am, sorry.

Nevertheless, the principle still holds. There's nothing in Python that 
prohibits two objects from being equal, but without them being 
interchangeable. As poorly written as my example was, it still holds: I 
just need to add a level of indirection.

>>> alist = [100, 111, 102, 103, 105, 104, 106, 108]
>>> indices_of_odd_numbers = [alist.index(n) for n in alist if n%2]
>>> if Decimal('3') in indices_of_odd_numbers:
...     print alist[Decimal('3')]
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: list indices must be integers


Python does not promise that if x == y, you can use y anywhere you can 
use x. Nor should it. Paul's declaration of abuse of __eq__ is 
unfounded.



-- 
Steven D'Aprano


More information about the Python-Dev mailing list