i want to know about the copy.deecopy( ) function in python
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: -
>>> 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']
-
>>>
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: -
>>> 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: 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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |