UserDict and UserList issue (bug?_

Thomas Wouters thomas at xs4all.nl
Fri Jun 30 21:06:50 EDT 2000


On Fri, 30 Jun 2000 16:54:37 -0700, Norman Shelley
<Norman_Shelley-RRDN60 at email.sps.mot.com> wrote:
>in UserDict and UserList there is one or more places where a call to
>self's __class__() method is made, e.g.
>
>def __getslice__(self, i, j):
>  i = max(i, 0); j = max(j, 0)
>  # This class and its subclasses need more arguments then 0
>  userlist = self.__class__()
>  ....

>This blows causes major problems when either of these two classes are
>subclassed and the class which subclasses them requires creation
>arguments.  I think maybe a copy of self instead of a __class__() may be
>a more robust solution.

And how would you get a copy of self without creating a new instance of
self.__class__ ? You usually create a copy of a list by doing:

    mycopy = mylist[:]

Which calls __getslice__, which is exactly what you are complaining about
:-)

If you want to subclass UserList, either make sure you provide defaults for
all arguments to the initializer, or make sure you override all places where
self.__class__() may be called without your required arguments. #1 is
probably the best (and most portable) solution, and not just because of the
above issue: providing defaults for all initializer arguments allows your
object to be pickled, for instance ;)

But I dont know why you need those arguments, so YMMV.

Regards,
	Thomas.



More information about the Python-list mailing list