mirror of
https://github.com/python/cpython.git
synced 2025-10-14 02:43:49 +00:00
robustify UserList constructor -- will now accept any sequence
add test cases for non-UserList class, tuple, & string
This commit is contained in:
parent
074c3e62d1
commit
6a973c7118
2 changed files with 19 additions and 5 deletions
|
@ -1,13 +1,16 @@
|
||||||
"""A more or less complete user-defined wrapper around list objects."""
|
"""A more or less complete user-defined wrapper around list objects."""
|
||||||
|
|
||||||
class UserList:
|
class UserList:
|
||||||
def __init__(self, list=None):
|
def __init__(self, initlist=None):
|
||||||
self.data = []
|
self.data = []
|
||||||
if list is not None:
|
if initlist is not None:
|
||||||
if type(list) == type(self.data):
|
# XXX should this accept an arbitary sequence?
|
||||||
self.data[:] = list
|
if type(initlist) == type(self.data):
|
||||||
|
self.data[:] = initlist
|
||||||
|
elif isinstance(initlist, UserList):
|
||||||
|
self.data[:] = initlist.data[:]
|
||||||
else:
|
else:
|
||||||
self.data[:] = list.data[:]
|
self.data = list(initlist)
|
||||||
def __repr__(self): return repr(self.data)
|
def __repr__(self): return repr(self.data)
|
||||||
def __cmp__(self, other):
|
def __cmp__(self, other):
|
||||||
if isinstance(other, UserList):
|
if isinstance(other, UserList):
|
||||||
|
|
|
@ -18,6 +18,17 @@ uu0 = UserList(u0)
|
||||||
uu1 = UserList(u1)
|
uu1 = UserList(u1)
|
||||||
uu2 = UserList(u2)
|
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__
|
# Test __repr__
|
||||||
|
|
||||||
assert str(u0) == str(l0)
|
assert str(u0) == str(l0)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue