How to deepcopy a list of user defined lists?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Thu Jan 8 09:51:40 CET 2009


On Thu, 08 Jan 2009 13:29:37 +0530, srinivasan srinivas wrote:

> Hi,
> I have a class which is a subclass of builtin-type list.
> 
> 
#------------------------------------------------------------------------------
> class clist(list):
>     def __new__(cls, values, ctor):
>         val = []
>         for item in values:
>             item = ctor(item)
>             val.append(item)
>         
>         self = list.__new__(cls, val)
>         self.__values = val
>         self.__ctor = ctor
>         return self
> 
#-----------------------------------------------------------------------------------
> 
> I have a list of clists, say c1 = [clist1, clist2, clist3] How do i
> deepcopy this list? I tried using copy.deepcopy() method. But i got some
> errors. Please suggest me.


Don't tell us what the errors are, I love guessing games!!!

Let's see... is it TypeError because your class doesn't override the 
list.__init__ method?



>>> clist1 = clist([1,2,3], str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list() takes at most 1 argument (2 given)


Before you get to deepcopy, you actually need to have clist work 
correctly. Once you've done that, show us what error you get.



-- 
Steven



More information about the Python-list mailing list