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

David Mertz mertz at gnosis.cx
Sat Sep 14 19:04:03 CEST 2013


Perhaps being pedantic, but there is not necessarily ONE key in the
original collection which is equal to the search value:

>>> 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
...
>>> 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... 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).




On Sat, Sep 14, 2013 at 9:52 AM, David Mertz <mertz at gnosis.cx> wrote:

> One way you can spell it currently is:
>
> >>> getexact = lambda m, v: [x for x in m if x==v][0]
> >>> d = {2: 'a', 5.0: 'b'}
> >>> s = {2, 5.0}
> >>> getexact(d, 2.0)
> 2
> >>> getexact(d, 17)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "<stdin>", line 1, in <lambda>
> IndexError: list index out of range
> >>> getexact(s, 2.0)
> 2
>
> It's true that the exception leaves a little to be desired here.  If you
> actually want to use this function much, and worry about the extra equality
> comparisons in my one-line version, maybe:
>
> def getexact(m, v):
>     for x in m:
>         if x==v: return x
>     else:
>         raise KeyError(v)
>
> Like Victor, I'd want to see an actual use case before wanting these as
> extra methods of the actual data types.  My function versions have the
> advantage also that they work on ANY iterable, not only dict and set, and
> also hence have one spelling for the same conceptual operation.
>
>
> On Sat, Sep 14, 2013 at 8:09 AM, Serhiy Storchaka <storchaka at gmail.com>wrote:
>
>> I propose to add two methods:
>>
>> dict.getkey(key) returns original key stored in the dict which is equal
>> to specified key. E.g.
>>
>> >>> d = {2: 'a', 5.0: 'b'}
>> >>> d.getkey(2.0)
>> 2
>> >>> d.getkey(5)
>> 5.0
>> >>> d.getkey(17)
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> KeyError: 17
>>
>> set.get(value) returns original value stored in the set which is equal to
>> specified value. E.g.
>>
>> >>> s = {2, 5.0}
>> >>> s.get(2.0)
>> 2
>> >>> s.get(5)
>> 5.0
>> >>> s.get(17)
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> KeyError: 17
>>
>> ______________________________**_________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/**mailman/listinfo/python-ideas<https://mail.python.org/mailman/listinfo/python-ideas>
>>
>
>
>
> --
> Keeping medicines from the bloodstreams of the sick; food
> from the bellies of the hungry; books from the hands of the
> uneducated; technology from the underdeveloped; and putting
> advocates of freedom in prisons.  Intellectual property is
> to the 21st century what the slave trade was to the 16th.
>



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130914/45ff2cfa/attachment-0001.html>


More information about the Python-ideas mailing list