Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using __deepcopy__ on custom object in python?

I am a writing a custom Queue class that uses list as a composite attribute. I do not sublcass from list. my code is here. I get the error for deepcopy which I have pasted below. can someone help me about what I is going wrong? Thanks

from iterator import Iterator
class Abstractstruc(object):
    def __init__(self):
        assert False
    def __str__(self):
        return "<%s: %s>" %(self.__class__.__name__,self.container)

class Queue(Abstractstruc,Iterator):

    def __init__(self,value=[]):
        self.container=[]
        self.size=0
        self.concat(value)

    def add(self, data):
            self.container.append(data)

    def  remove(self):
        self.container.pop(0)


    def peek(self):
        return self.container[0]


    def __getitem__(self,index):
        return self.container[index]


    def __iter__(self):
        return Iterator(self.container)

    def concat(self,value):
        for i in value:
            self.add(i)

    def __bool__(self):
        return len(self.container)>0

    def __len__(self):
        return len(self.container)


    def __deepcopy__(self,memo):
        return Queue(copy.deepcopy(self.container,memo))


if __name__=='__main__':
    q3=Queue()

    li=[1,2,3]
    q3.add(li)
    print q3 
    print len(q3)

    q4=copy.deepcopy(q3)
    q3.peek()[0]=100


    print "after modifying"
    print q3
    print "q4 = ", q4

output:

<Queue: [[1, 2, 3]]>
1
Traceback (most recent call last):
  File "test.py", line 56, in <module>
    q4=copy.deepcopy(q3)
NameError: name 'copy' is not defined
like image 794
brain storm Avatar asked Nov 25 '13 19:11

brain storm


People also ask

How do you Deepcopy an object in Python?

To make a deep copy, use the deepcopy() function of the copy module. In a deep copy, copies are inserted instead of references to objects, so changing one does not change the other.

What is Deepcopy () in Python?

What is Deep copy in Python? A deep copy creates a new compound object before inserting copies of the items found in the original into it in a recursive manner. It means first constructing a new collection object and then recursively populating it with copies of the child objects found in the original.

What is the difference between copy copy () and copy Deepcopy ()?

copy() create reference to original object. If you change copied object - you change the original object. . deepcopy() creates new object and does real copying of original object to new one. Changing new deepcopied object doesn't affect original object.

How do I make a Deepcopy list in Python?

You use copy. deepcopy(...) for deep copying a list. deepcopy(x, memo=None, _nil=[]) Deep copy operation on arbitrary Python objects.


1 Answers

You need to import the copy module:

import copy

The error message:

Traceback (most recent call last):
  File "test.py", line 56, in <module>
    q4=copy.deepcopy(q3)
NameError: name 'copy' is not defined

NameError is raised when Python does not know what the bare name copy refers to.

like image 124
unutbu Avatar answered Sep 28 '22 21:09

unutbu