UserList.__getslice__(): copy.copy(self.data) vs. self.__class__(self.data).

Alex alex at somewhere.round.here
Tue Mar 14 23:37:00 CET 2000


> Though this follows the guidelines outlined in the reference manual,
> it has an interesting side effect: it instantiates a new object of the
> same class but it loses the current values of all attributes.
> 
> Is this desireable behavior?  Personally, I don't believe that it is.
> My thinking is that the current internal state of the class should
> pass to the newly instantiated object.

Sometimes, that's what you want, and, in my experience, sometimes not.
UserList is intended as a base class, so you can override any methods
that aren't doing what you want.  For instance, you could do something
like this:

from UserList import UserList

class NewUserList (UserList):

    def __getslice__ (self, i, j):
        i = max(i, 0); j = max(j, 0)
        userlist = copy.copy(self)
        userlist.data[:] = self.data[i:j]
        return userlist

... and use NewUserList in your project instead.

Hope this helps.
Alex.



More information about the Python-list mailing list