]> granicus.if.org Git - python/commitdiff
Make the underlying data structure more private.
authorRaymond Hettinger <python@rcn.com>
Tue, 3 Mar 2009 20:47:29 +0000 (20:47 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 3 Mar 2009 20:47:29 +0000 (20:47 +0000)
Lib/collections.py

index 28c8f1093e3f638ae32962f28509579e81110bbc..4028983685304e20b7ac8cd1ee410594f03a0b99 100644 (file)
@@ -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