diff lookup_list.py @ 19:57f4015e2e09

Iterators extend LookupList
author bergstrj@iro.umontreal.ca
date Thu, 27 Mar 2008 01:59:44 -0400
parents 759d17112b23
children 266c68cb6136
line wrap: on
line diff
--- a/lookup_list.py	Thu Mar 27 00:19:16 2008 -0400
+++ b/lookup_list.py	Thu Mar 27 01:59:44 2008 -0400
@@ -47,20 +47,28 @@
             if key in self._name2index:
                 self._values[self._name2index[key]]=value
             else:
-                self._name2index[key]=len(self)
-                self._values.append(value)
-                self._names.append(key)
+                raise KeyError(key)
 
     def __getattr__(self,name):
-        return self._values[self._name2index[name]]
+        try:
+            return self._values[self._name2index[name]]
+        except KeyError, e:
+            raise AttributeError(name)
 
-    def __setattr__(self,name,value):
-        if name in self._name2index:
-            self._values[self._name2index[name]]=value
-        else:
-            self._name2index[name]=len(self)
-            self._values.append(value)
-            self._names.append(name)
+    if 0:
+        # This makes subclassing horrible, just call append_keyval if it's
+        # really what you want to do.
+        # -JB
+        def __setattr__(self,name,value):
+            if name in self._name2index:
+                self._values[self._name2index[name]]=value
+            else:
+                raise AttributeError(name)
+
+    def append_keyval(self, key, value):
+        self._name2index[key]=len(self)
+        self._values.append(value)
+        self._names.append(key)
 
     def __len__(self):
         return len(self._values)