subclassing 'list'

Chris Rebert clp2 at
Tue Jan 6 21:43:57 CET 2009

On Tue, Jan 6, 2009 at 12:34 PM, akineko <akineko at> wrote:
> Hello everyone,
> I'm creating a class which is subclassed from list (Bulit-in type).
> It works great.
> However, I'm having a hard time finding a way to set a new value to
> the object (within the class).
> There are methods that alter a part of the object (ex. __setitem__()).
> But I couldn't find any method that can replace the value of the
> object.
> I wanted to do something like the following:
> class Mylist(list):
>    def arrange(self):
>        new_value = ....
>        list.self.__assign__.(self, new_value)

If you mean you want to replace the contents of the list with that of
another list, just do:

def arrange(self):
    self.clear() #empty the list
    self.extend(new_list) #append the contents of other list

If you instead mean that you want the object to somehow completely
"become" or be replaced by another object, that's not possible (short
of proxying). Smalltalk has a `become` method, but not Python (it's
pretty deep black magic anyway). If you gave more info about _why_ you
want to do that, someone could probably suggest an alternative.

Follow the path of the Iguana...

More information about the Python-list mailing list