i want to know about the copy.deecopy( ) function in python
2 3784
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: -
>>> listA = ['spam', 'spam', 'spam', 'spam',]
-
>>> listB = listA # a reference (not a new object)
-
>>> listB[-1] = 'eggs'
-
>>> print listA
-
['spam', 'spam', 'spam', 'eggs']
-
>>> import copy
-
>>> listC = copy.copy(listA) # create a new object
-
>>> listC[-1] = 'toast'
-
>>> print listA
-
['spam', 'spam', 'spam', 'eggs']
-
>>> print listC
-
['spam', 'spam', 'spam', 'toast']
-
>>> listABC =[listA, listB, listC]
-
>>> listDEF = listABC # copy.copy() would also give a list of references (not new objects)
-
>>> listDEF[0][-1] = 'jelly' # last element of listA
-
>>> print listA
-
['spam', 'spam', 'spam', 'jelly']
-
>>> listXYZ = copy.deepcopy(listABC) # create a list of new objects
-
>>> listXYZ[0][-1] = 'spam' # NOT the last element of listA
-
>>> print listA
-
['spam', 'spam', 'spam', 'jelly']
-
>>>
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: -
>>> listA = ['spam', 'spam', 'spam', 'spam',]
-
>>> listB = listA # a reference (not a new object)
-
>>> listB[-1] = 'eggs'
-
>>> print listA
-
['spam', 'spam', 'spam', 'eggs']
-
>>> import copy
-
>>> listC = copy.copy(listA) # create a new object
-
>>> listC[-1] = 'toast'
-
>>> print listA
-
['spam', 'spam', 'spam', 'eggs']
-
>>> print listC
-
['spam', 'spam', 'spam', 'toast']
-
>>> listABC =[listA, listB, listC]
-
>>> listDEF = listABC # copy.copy() would also give a list of references (not new objects)
-
>>> listDEF[0][-1] = 'jelly' # last element of listA
-
>>> print listA
-
['spam', 'spam', 'spam', 'jelly']
-
>>> listXYZ = copy.deepcopy(listABC) # create a list of new objects
-
>>> listXYZ[0][-1] = 'spam' # NOT the last element of listA
-
>>> print listA
-
['spam', 'spam', 'spam', 'jelly']
-
>>>
I just had to prove to myself that I knew what I was saying was true: -
['spam', 'spam', 'spam', 'jelly']
-
>>> listDEF = copy.copy(listABC)
-
>>> listDEF[0][-1] = 'spam' # last element of listA
-
>>> print listA
-
['spam', 'spam', 'spam', 'spam']
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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
>>>
|
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)...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |