471,325 Members | 1,450 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 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 3436
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

Post your reply

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

Similar topics

6 posts views Thread by Alexander Zatvornitskiy | last post: by
30 posts views Thread by franky.backeljauw | last post: by
reply views Thread by Joshua Ginsberg | last post: by
4 posts views Thread by fperfect13 | last post: by
26 posts views Thread by saxenavaibhav17 | last post: by
reply views Thread by rosydwin | last post: by

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.