summaryrefslogtreecommitdiffstats
path: root/Lib/collections.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-12-31 23:16:17 (GMT)
committerRaymond Hettinger <python@rcn.com>2010-12-31 23:16:17 (GMT)
commita673b1fd0e6396e587a99ec9b2bec6b40132557a (patch)
tree08f806a0ce4cc5b7c2e4adefe60f6fcf19d3282e /Lib/collections.py
parented13853e5dd22a85131b34707c6087677eec642a (diff)
downloadcpython-a673b1fd0e6396e587a99ec9b2bec6b40132557a.zip
cpython-a673b1fd0e6396e587a99ec9b2bec6b40132557a.tar.gz
cpython-a673b1fd0e6396e587a99ec9b2bec6b40132557a.tar.bz2
Fix OrderedDict.setdefault() to work for subclasses that define __missing__().
Diffstat (limited to 'Lib/collections.py')
-rw-r--r--Lib/collections.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 061106b..c69f4ca 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -171,7 +171,6 @@ class OrderedDict(dict, MutableMapping):
size += sizeof(self.__root) * n # proxy objects
return size
- setdefault = MutableMapping.setdefault
update = MutableMapping.update
pop = MutableMapping.pop
keys = MutableMapping.keys
@@ -179,6 +178,13 @@ class OrderedDict(dict, MutableMapping):
items = MutableMapping.items
__ne__ = MutableMapping.__ne__
+ 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:
+ return self[key]
+ self[key] = default
+ return default
+
@_recursive_repr()
def __repr__(self):
'od.__repr__() <==> repr(od)'