mirror of
https://github.com/python/cpython.git
synced 2025-10-17 20:28:43 +00:00
Backport r87613 to make OrderedDict subclassing match dict subclassing.
This commit is contained in:
parent
db0ef2b5e5
commit
1d879f6852
2 changed files with 49 additions and 5 deletions
|
@ -21,7 +21,7 @@ from itertools import repeat as _repeat, chain as _chain, starmap as _starmap
|
|||
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.
|
||||
|
@ -50,7 +50,7 @@ class OrderedDict(dict, MutableMapping):
|
|||
self.__root = root = _Link() # sentinel node for the doubly linked list
|
||||
root.prev = root.next = root
|
||||
self.__map = {}
|
||||
self.update(*args, **kwds)
|
||||
self.__update(*args, **kwds)
|
||||
|
||||
def clear(self):
|
||||
'od.clear() -> None. Remove all items from od.'
|
||||
|
@ -109,13 +109,29 @@ class OrderedDict(dict, MutableMapping):
|
|||
return (self.__class__, (items,), inst_dict)
|
||||
return self.__class__, (items,)
|
||||
|
||||
setdefault = MutableMapping.setdefault
|
||||
update = MutableMapping.update
|
||||
pop = MutableMapping.pop
|
||||
update = __update = MutableMapping.update
|
||||
keys = MutableMapping.keys
|
||||
values = MutableMapping.values
|
||||
items = MutableMapping.items
|
||||
|
||||
__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:
|
||||
return self[key]
|
||||
self[key] = default
|
||||
return default
|
||||
|
||||
def popitem(self, last=True):
|
||||
'''od.popitem() -> (k, v), return and remove a (key, value) pair.
|
||||
Pairs are returned in LIFO order if last is true or FIFO order if false.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue