Changes to UserList.py
Jakob Schiotz
schiotz1 at yahoo.com
Thu Jan 6 11:57:08 EST 2000
--- Yesterday, I <schiotz1 at yahoo.com> wrote:
> Dear Python Gurus,
>
> Apparently, classes derived from UserList can be initialized with
> real
> lists and with instances themselves derived from UserList but not
> with
> other list-like objects (i.e. objects defining the methods you would
> expect of a list).
The solution is probably to fix UserList.py. I have attached a fix I
have submitted to GvR. Here is a short description:
The problem
was that UserLists could not be initialized from any sequence, only
from true lists and other UserLists. Furthermore, __init__
incorrectly assumed that anything not a true list is a UserList,
causing a confusing AttributeError. __setslice__, __add__ and
__radd__ are more tolerant, they test if their argument is a
UserList or a true list, and if not they attempt to convert it to a
list with list(s). The extend method, however, had the same problem
as __init__.
__init__ was changed from
def __init__(self, list=None):
self.data = []
if list is not None:
if type(list) == type(self.data):
self.data[:] = list
else:
self.data[:] = list.data[:]
to
def __init__(self, lst=None):
self.data = []
if lst is not None:
if isinstance(lst, UserList):
self.data[:] = lst.data[:]
elif type(lst) == type(self.data):
self.data[:] = lst
else:
self.data = list(lst)
extend was changes similarly.
> [ ... ] and then
> the build-in function list() should be extended to work on any object
> having a __getitem__ method. The latter change should probably be
> done
> anyway in a later version of Python.
Ooops, I managed to fool myself into thinking that list(x) didn't work,
when the problem was that I used it in __init__ above, but had kept the
argument name as "list" thus hiding the function.
Best regards,
Jakob Schiotz
=====
Jakob Schiotz, CAMP and Department of Physics, Tech. Univ. of Denmark,
DK-2800 Lyngby, Denmark. http://www.fysik.dtu.dk/~schiotz/
This email address is used for newsgroups and mailing lists
(spam protection). Official email: schiotz @ fysik . dtu . dk
When spammed too much, I'll move on to schiotz2 at yahoo.com
__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: UserList.diff
Type: application/x-unknown
Size: 1326 bytes
Desc: UserList.diff
URL: <http://mail.python.org/pipermail/python-list/attachments/20000106/ede08438/attachment.bin>
More information about the Python-list
mailing list