[Python-checkins] peps: Propose a getitem() method

antoine.pitrou python-checkins at python.org
Fri Sep 13 23:48:38 CEST 2013


http://hg.python.org/peps/rev/4a6b87d0ee91
changeset:   5110:4a6b87d0ee91
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Fri Sep 13 23:48:33 2013 +0200
summary:
  Propose a getitem() method

files:
  pep-0455.txt |  17 +++++++++++++++++
  1 files changed, 17 insertions(+), 0 deletions(-)


diff --git a/pep-0455.txt b/pep-0455.txt
--- a/pep-0455.txt
+++ b/pep-0455.txt
@@ -95,6 +95,23 @@
    >>> sorted(d.items())
    [('Bar', 2), ('Foo', 1)]
 
+Getting the original key
+------------------------
+
+TransformDict also features a lookup method returning the stored key
+together with the corresponding value::
+
+    >>> d = TransformDict(str.casefold, {'Foo': 1})
+    >>> d.getitem('FOO')
+    ('Foo', 1)
+    >>> d.getitem('bar')
+    Traceback (most recent call last):
+      File "<stdin>", line 1, in <module>
+    KeyError: 'bar'
+
+The method name ``getitem()`` mirrors the standard ``popitem()`` method
+on mutable mappings.
+
 
 Alternative proposals and questions
 ===================================

-- 
Repository URL: http://hg.python.org/peps


More information about the Python-checkins mailing list