[Python-ideas] Add dict.getkey() and set.get()

Terry Reedy tjreedy at udel.edu
Sat Sep 14 19:56:25 CEST 2013

On 9/14/2013 1:04 PM, David Mertz wrote:
> Perhaps being pedantic, but there is not necessarily ONE key in the
> original collection which is equal to the search value:

Sets presume that == is an equivalence relation. When that is not true, 
as for such FuzzyNumbers (which break transitivity), they should not be 
used with sets at all, as many operations will be somewhat broken. For 
one thing, the composition of such 'sets' will depend on the order of 

>  >>> d = {2: 'a', 5.0: 'b', 7.2: 'low', 7.6: 'high'}
>  >>> class FuzzyNumber(float):
> ...     def __eq__(self, other):
> ...         return abs(self-other) < 0.5

Better to call this method 'similar' or 'is_similar', since similarity 
is not expected to be transitive.

>  >>> fn = FuzzyNumber(7.5)
>  >>> getexact(d, fn) # What gets returned here?!
> 7.6
> ANY implementation of this idea would either have to pick the arbitrary
> first match, or ... well, something else.  Equality isn't actually
> transitive across all Python objects...

Except for NaNs, the non-floats called floats for the benefit of 
languages with typed operations, I believe equality is (at least as far 
as possible) transitive for the built-in classes as delivered. We fixed
   0.0 == 0 == Decimal(0) != 0.0
because of the problems caused by the non-transitivity. Avoiding 
breaking transitivity was one of the design constraints of the Enums.

 > and even my quick example isn't
> a completely absurd data type (it would need to be fleshed out better,
> but a FuzzyNumber could well have sensible purposes).

The only absurd thing is calling similarity 'equality' ;=).

Terry Jan Reedy

More information about the Python-ideas mailing list