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

Terry Reedy tjreedy at udel.edu
Sat Sep 14 20:03:51 CEST 2013


On 9/14/2013 11:09 AM, Serhiy Storchaka 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

If sets had get() as you described, there would be no need for 
dict.getkey as long as the set-like key view had get(). This would be 
the appropriate place since get() has nothing to do with the values but 
only the set of keys.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list