473,473 Members | 2,316 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

copy.deepcopy( ) vs copy.copy( )

1 New Member
i want to know about the copy.deecopy( ) function in python
Jun 26 '07 #1
2 3834
bartonc
6,596 Recognized Expert Expert
i want to know about the copy.deecopy( ) function in python
deepcopy() is for making copies of iterables which may contain iterables. This is as compared to copy() that makes "shallow" copies of (say) lists that are inside a list. To understand the difference, you need to get the whole "pass by reference" thing. I'll try to illustrate:
Expand|Select|Wrap|Line Numbers
  1. >>> listA = ['spam', 'spam', 'spam', 'spam',]
  2. >>> listB = listA    # a reference (not a new object)
  3. >>> listB[-1] = 'eggs'
  4. >>> print listA
  5. ['spam', 'spam', 'spam', 'eggs']
  6. >>> import copy
  7. >>> listC = copy.copy(listA)    # create a new object
  8. >>> listC[-1] = 'toast'
  9. >>> print listA
  10. ['spam', 'spam', 'spam', 'eggs']
  11. >>> print listC
  12. ['spam', 'spam', 'spam', 'toast']
  13. >>> listABC =[listA, listB, listC]
  14. >>> listDEF = listABC    # copy.copy() would also give a list of references (not new objects)
  15. >>> listDEF[0][-1] = 'jelly' # last element of listA
  16. >>> print listA
  17. ['spam', 'spam', 'spam', 'jelly']
  18. >>> listXYZ = copy.deepcopy(listABC)  # create a list of new objects
  19. >>> listXYZ[0][-1] = 'spam' # NOT the last element of listA
  20. >>> print listA
  21. ['spam', 'spam', 'spam', 'jelly']
  22. >>> 
Jun 26 '07 #2
bartonc
6,596 Recognized Expert Expert
deepcopy() is for making copies of iterables which may contain iterables. This is as compared to copy() that makes "shallow" copies of (say) lists that are inside a list. To understand the difference, you need to get the whole "pass by reference" thing. I'll try to illustrate:
Expand|Select|Wrap|Line Numbers
  1. >>> listA = ['spam', 'spam', 'spam', 'spam',]
  2. >>> listB = listA    # a reference (not a new object)
  3. >>> listB[-1] = 'eggs'
  4. >>> print listA
  5. ['spam', 'spam', 'spam', 'eggs']
  6. >>> import copy
  7. >>> listC = copy.copy(listA)    # create a new object
  8. >>> listC[-1] = 'toast'
  9. >>> print listA
  10. ['spam', 'spam', 'spam', 'eggs']
  11. >>> print listC
  12. ['spam', 'spam', 'spam', 'toast']
  13. >>> listABC =[listA, listB, listC]
  14. >>> listDEF = listABC    # copy.copy() would also give a list of references (not new objects)
  15. >>> listDEF[0][-1] = 'jelly' # last element of listA
  16. >>> print listA
  17. ['spam', 'spam', 'spam', 'jelly']
  18. >>> listXYZ = copy.deepcopy(listABC)  # create a list of new objects
  19. >>> listXYZ[0][-1] = 'spam' # NOT the last element of listA
  20. >>> print listA
  21. ['spam', 'spam', 'spam', 'jelly']
  22. >>> 
I just had to prove to myself that I knew what I was saying was true:
Expand|Select|Wrap|Line Numbers
  1. ['spam', 'spam', 'spam', 'jelly']
  2. >>> listDEF = copy.copy(listABC)
  3. >>> listDEF[0][-1] = 'spam' # last element of listA
  4. >>> print listA
  5. ['spam', 'spam', 'spam', 'spam']
  6. >>> 
Jun 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Alexander Zatvornitskiy | last post by:
Hello! I have trouble with copy/deepcopy. It seems, I just don't understand something. Please explain where things goes wrong and how to do it the right way. I have one class: class...
30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
0
by: Joshua Ginsberg | last post by:
Howdy -- I have a class that has an attribute that is a dictionary that contains an object that has a kword argument that is a lambda. Confused yet? Simplified example: import copy class...
2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
4
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx)...
26
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
4
by: Emin | last post by:
Dear experts, I got some unexpected behavior in getattr and copy.deepcopy (see transcript below). I'm not sure if this is actually a bug in copy.deepcopy or if I'm doing something too magical...
0
bartonc
by: bartonc | last post by:
I was playing around with the Simple Metaclassing thread and found something odd: >>> class aClass: ... classVar1 = 'hello' ... def __init__(self, arg1): ... self.instVar1 = arg1...
1
by: lawbaal | last post by:
As the title tells, the detail is: There is a dictionary to be deepcopied. it has 100 key-value pairs. every value is a list of 200000 float numbers. I'm perform deepcopy to everyone of the value...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.