[Python-checkins] r83328 - python/branches/py3k/Doc/library/collections.rst

raymond.hettinger python-checkins at python.org
Sat Jul 31 12:14:41 CEST 2010


Author: raymond.hettinger
Date: Sat Jul 31 12:14:41 2010
New Revision: 83328

Log:
Document how to change OrderedDict update order from first to last.

Modified:
   python/branches/py3k/Doc/library/collections.rst

Modified: python/branches/py3k/Doc/library/collections.rst
==============================================================================
--- python/branches/py3k/Doc/library/collections.rst	(original)
+++ python/branches/py3k/Doc/library/collections.rst	Sat Jul 31 12:14:41 2010
@@ -938,6 +938,18 @@
 are deleted.  But when new keys are added, the keys are appended
 to the end and the sort is not maintained.
 
+It is also straight-forward to create an ordered dictionary variant
+that the remembers the order the keys were *last* inserted.
+If a new entry overwrites an existing entry, the
+original insertion position is changed and moved to the end::
+
+    class LastUpdatedOrderedDict(OrderedDict):
+        'Store items is the order the keys were last added'
+        def __setitem__(self, key, value):
+            if key in self:
+                del self[key]
+            OrderedDict.__setitem__(self, key, value)
+
 
 :class:`UserDict` objects
 -------------------------


More information about the Python-checkins mailing list