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

Victor Stinner victor.stinner at gmail.com
Sat Sep 14 18:19:48 CEST 2013


What is the use case of such methods? Why not using dict.kets() and
tuple(set)?

Victor
 Le 14 sept. 2013 17:09, "Serhiy Storchaka" <storchaka at gmail.com> a écrit :

> 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>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130914/b1fb480b/attachment.html>


More information about the Python-ideas mailing list