diff options
author | Raymond Hettinger <python@rcn.com> | 2011-01-01 23:51:55 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2011-01-01 23:51:55 (GMT) |
commit | 345c49b16b093399ad504711d55bad700ad7a33b (patch) | |
tree | 4bba38bffe4a5f85fbe9dfc3b1b95b980842d3ac /Lib/collections.py | |
parent | 32062e9be78ee49b563d1b7eb290a00e6fe16c0f (diff) | |
download | cpython-345c49b16b093399ad504711d55bad700ad7a33b.zip cpython-345c49b16b093399ad504711d55bad700ad7a33b.tar.gz cpython-345c49b16b093399ad504711d55bad700ad7a33b.tar.bz2 |
Fix OrderedDic.pop() to work for subclasses that define __missing__().
Diffstat (limited to 'Lib/collections.py')
-rw-r--r-- | Lib/collections.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index d0a44c2..36ee18a 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -22,7 +22,7 @@ from reprlib import recursive_repr as _recursive_repr class _Link(object): __slots__ = 'prev', 'next', 'key', '__weakref__' -class OrderedDict(dict, MutableMapping): +class OrderedDict(dict): 'Dictionary that remembers insertion order' # An inherited dict maps keys to values. # The inherited dict provides __getitem__, __len__, __contains__, and get. @@ -172,12 +172,22 @@ class OrderedDict(dict, MutableMapping): return size update = __update = MutableMapping.update - pop = MutableMapping.pop keys = MutableMapping.keys values = MutableMapping.values items = MutableMapping.items __ne__ = MutableMapping.__ne__ + __marker = object() + + def pop(self, key, default=__marker): + if key in self: + result = self[key] + del self[key] + return result + if default is self.__marker: + raise KeyError(key) + return default + def setdefault(self, key, default=None): 'OD.setdefault(k[,d]) -> OD.get(k,d), also set OD[k]=d if k not in OD' if key in self: |