clear derived list

Alex Martelli aleax at aleax.it
Thu Mar 13 14:02:27 EST 2003


Steven Taschuk wrote:

> Quoth John Hunter:
>   [...]
>> class mylist(list):
>>     def set(self, seq):
>>         # flush the old list
>>         [self.pop() for i in range(len(self))]
>>         self.extend(seq)
> 
> How about using slice assignment?
> 
> def set(self, seq):
> self[:] = seq

This will break, while the original code would work, if
seq was a non-list sequence.  Easy fix:

    self[:] = list(seq)


Alex





More information about the Python-list mailing list