UserDict and UserList issue (bug?_

Norman Shelley Norman_Shelley-RRDN60 at email.sps.mot.com
Mon Jul 3 11:58:14 EDT 2000


Thomas Wouters wrote:

> 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__ ?

Simple.

import copy
...

userlist = copy.copy(self)



> 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