]> granicus.if.org Git - python/commitdiff
robustify UserList constructor -- will now accept any sequence
authorJeremy Hylton <jeremy@alum.mit.edu>
Fri, 31 Mar 2000 00:17:46 +0000 (00:17 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Fri, 31 Mar 2000 00:17:46 +0000 (00:17 +0000)
add test cases for non-UserList class, tuple, & string

Lib/UserList.py
Lib/test/test_userlist.py

index 1680327f5728f77b546e57c4d892cd83ccf86cc9..7bd02986dcc394a5b74ad8510ef7a72745547312 100644 (file)
@@ -1,13 +1,16 @@
 """A more or less complete user-defined wrapper around list objects."""
 
 class UserList:
-    def __init__(self, list=None):
+    def __init__(self, initlist=None):
         self.data = []
-        if list is not None:
-            if type(list) == type(self.data):
-                self.data[:] = list
+        if initlist is not None:
+            # XXX should this accept an arbitary sequence?
+            if type(initlist) == type(self.data):
+                self.data[:] = initlist
+            elif isinstance(initlist, UserList):
+                self.data[:] = initlist.data[:]
             else:
-                self.data[:] = list.data[:]
+                self.data = list(initlist)
     def __repr__(self): return repr(self.data)
     def __cmp__(self, other):
         if isinstance(other, UserList):
index b6846899d4211ea97f041e20a5543ff3129f14d5..6c627c01c289b52db25735c26c566696de348bbf 100644 (file)
@@ -18,6 +18,17 @@ uu0 = UserList(u0)
 uu1 = UserList(u1)
 uu2 = UserList(u2)
 
+v = UserList(tuple(u))
+class OtherList:
+    def __init__(self, initlist):
+        self.__data = initlist
+    def __len__(self):
+        return len(self.__data)
+    def __getitem__(self, i):
+        return self.__data[i]
+v0 = UserList(OtherList(u0))
+vv = UserList("this is also a sequence")
+
 # Test __repr__
 
 assert str(u0) == str(l0)