[Python-ideas] Add dict.getkey() and set.get()
Terry Reedy
tjreedy at udel.edu
Sat Sep 14 22:22:09 CEST 2013
On 9/14/2013 2:20 PM, David Mertz wrote:
> I'm not putting any huge weight in my toy class. But notice that it
> deliberately is NOT hashable, hence cannot make it into a set (or dict key):
...
> >>> [x for x in (1,2, 7.2, 7.3, 7.9, 8.0, 9) if ref_val.close_to(x)]
...
> But anyway, whether or not my FuzzyNumber class is a *good* idea, it is
> something that end users *could* do as long as we give them an .__eq__()
> magic method to play with. Hence a 'getexact()' function or a
> dict.getkey() method would have to do SOMETHING when presented with such
> a transitivity-of-equality-breaking object.
I would expect the proposed set/dict methods to work by hashing the
target. Otherwise, they would be no be justified as *methods*, as the
generic function should be, as you said, a function.
The generic 'iterate and return the first item matching the target' will
either return the first match or do whatever the else: clause dictates.
I do not see why you think there is a special problem with this. There
is nothing special about equality versus any other match predicate.
Transitivity is irrelevant here. On the other hand, symmetry is a
concern, as 'item == target' and 'target == item' could be different and
the result of the function would depend on which is used.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list