472,981 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,981 software developers and data experts.

copy.deepcopy( ) vs copy.copy( )

i want to know about the copy.deecopy( ) function in python
Jun 26 '07 #1
2 3784
bartonc
6,596 Expert 4TB
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 Expert 4TB
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.