How to deepcopy a list of user defined lists?

Discussion in 'Python' started by srinivasan srinivas, Jan 8, 2009.

  1. 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.

    Thanks,
    Srini


    Add more friends to your messenger and enjoy! Go to http://messenger.yahoo.com/invite/
     
    srinivasan srinivas, Jan 8, 2009
    #1
    1. Advertising

  2. 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
     
    Steven D'Aprano, Jan 8, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    496
    Thorsten Ottosen
    Oct 14, 2004
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    410
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. yomgui

    list of lists of lists ....

    yomgui, Jul 28, 2006, in forum: Python
    Replies:
    6
    Views:
    315
    yomgui
    Jul 31, 2006
  4. Oodini
    Replies:
    1
    Views:
    1,778
    Keith Thompson
    Sep 27, 2005
  5. Replies:
    1
    Views:
    504
    Sion Arrowsmith
    Jul 10, 2008
Loading...

Share This Page