diff options
author | Raymond Hettinger <python@rcn.com> | 2009-03-03 20:47:29 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-03-03 20:47:29 (GMT) |
commit | 08c70cf53cbb0307ac527c754b6abb8c6274dd29 (patch) | |
tree | 87e666e0a178a91c3102dfaeda84b05ff7949381 | |
parent | b0d56afc84f4a7dacd8296e9203b051f019e2c3a (diff) | |
download | cpython-08c70cf53cbb0307ac527c754b6abb8c6274dd29.zip cpython-08c70cf53cbb0307ac527c754b6abb8c6274dd29.tar.gz cpython-08c70cf53cbb0307ac527c754b6abb8c6274dd29.tar.bz2 |
Make the underlying data structure more private.
-rw-r--r-- | Lib/collections.py | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index 28c8f10..4028983 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -22,40 +22,44 @@ class OrderedDict(dict, MutableMapping): def __init__(self, *args, **kwds): if len(args) > 1: raise TypeError('expected at most 1 arguments, got %d' % len(args)) - if not hasattr(self, '_keys'): - self._keys = [] + try: + self.__keys + except AttributeError: + # Note the underlying data structure for this class is likely to + # change in the future. Do not rely on it or access it directly. + self.__keys = [] self.update(*args, **kwds) def clear(self): - del self._keys[:] + del self.__keys[:] dict.clear(self) def __setitem__(self, key, value): if key not in self: - self._keys.append(key) + self.__keys.append(key) dict.__setitem__(self, key, value) def __delitem__(self, key): dict.__delitem__(self, key) - self._keys.remove(key) + self.__keys.remove(key) def __iter__(self): - return iter(self._keys) + return iter(self.__keys) def __reversed__(self): - return reversed(self._keys) + return reversed(self.__keys) def popitem(self): if not self: raise KeyError('dictionary is empty') - key = self._keys.pop() + key = self.__keys.pop() value = dict.pop(self, key) return key, value def __reduce__(self): items = [[k, self[k]] for k in self] inst_dict = vars(self).copy() - inst_dict.pop('_keys', None) + inst_dict.pop('__keys', None) return (self.__class__, (items,), inst_dict) setdefault = MutableMapping.setdefault |