473,379 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Legitimacy of deepcopy

There is a list of custom objects. I want do duplicate this list to modify
objects in new list and then compare old and new. I could do it with
deepcopy

class foo:
def __init__(self, str):
self.str = str
old = [foo('str1'), foo('str2')]

import copy
new = copy.deepcopy(old)

But I've found very few deepcopy uses in Python library and other programs,
so I wonder it's possible to do it other, more elegant way.

Jul 18 '05 #1
2 1493
Eugeni Doljenko wrote:
There is a list of custom objects. I want do duplicate this list to modify
objects in new list and then compare old and new. I could do it with
deepcopy

class foo:
def __init__(self, str):
self.str = str
old = [foo('str1'), foo('str2')]

import copy
new = copy.deepcopy(old)

But I've found very few deepcopy uses in Python library and other programs,
so I wonder it's possible to do it other, more elegant way.


The problem with deepcopy is that it is impossible to do "correctly,"
since "correctly" depends on where your data structure abstractions are.
A simple example:

steve = People('Steve')
joe = People(steve)
mystruct = [(steve, joe, Dollars(5)), steve]

Now, should a deepcopy have two distinct steves, or one? Is the $5.00
in the new structure "the same as" another $5.00 or not? Sometime you
mean the identity of an object, and sometimes you mean it as a value,
and no general purpose function named deepcopy can guess where to stop
copying.

--
-Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #2
Scott David Daniels <Sc***********@Acm.Org> writes:
The problem with deepcopy is that it is impossible to do "correctly,"
since "correctly" depends on where your data structure abstractions are.
Well, just because there's more than one way to do it, need not mean
that a particular solution isn't a correct one. The semantics of
deepcopy are well-defined and I think perfectly valid. Now, if they
don't fit a particular need then it doesn't fit the need, but that's
up to the user to decide on a case by case basis.

To the original poster, we used deepcopy in a number of interface
points where we need to ensure isolation of internal data structures
versus copies of that information returned to callers. For example,
we can't just return a reference to an internal list or dictionary
which would let the caller later mutate our internal object without
our knowledge. For that, deepcopy works just the way we need, since
our primary goal is to avoid mutation of our original objects.

In most of our use cases, the objects in question are providing data
storage (either in-memory or as a simulation for a database) and need
strict isolation between their internal storage and what callers see
as the objects being returned.

In such cases, I really don't see any good solution in lieu of
deepcopy, and in fact deepcopy (as covered below) is probably already
a pretty good minimal solution in terms of actual work it does. It's
certainly a legitimate need and deepcopy is certainly a legitimate
implementation in my eyes. (I'll grant you that sometimes it feels
strange forcing object copies when you don't normally find yourself
needing to, but when yoiu want copies, you want copies :-))
A simple example:

steve = People('Steve')
joe = People(steve)
mystruct = [(steve, joe, Dollars(5)), steve]

Now, should a deepcopy have two distinct steves, or one? Is the $5.00
in the new structure "the same as" another $5.00 or not? Sometime you
mean the identity of an object, and sometimes you mean it as a value,
and no general purpose function named deepcopy can guess where to stop
copying.


Valid questions, but just because they can have different answers need
not mean that deepcopy can't pick its own set of answers and stick to
them. In the deepcopy case, it will have one distinct steve
(referenced twice from the new list, and once from within the new joe
within the list) because deepcopy keeps a memo list of objects it has
already copied (to avoid problems with recursion). By doing so it
achieves the primary goal of ensuring that the steve references in the
copy point to a distinct steve from the original (since it is the
original container object you are deep copying), but they remain
internally consistent with how they were used in the original object.

Likewise, the Dollars() instance in the copy will be distinct from
that in the original.

A subtlety (which I'm guessing you are referring to by your identity
comment) is that a deepcopy (IMHO) is aimed at ensuring that the new
copy does not have the ability to mutate objects in the original.
Towards that end, there's no real need to create copies of immutable
objects since they can't be mutated in the first place. So immutable
objects such as numbers, tuples, etc.. will simply have references to
the same object placed in the copy (unless overridden by
__deepcopy__). This is no different to how a copy.copy() of an
immutable object will return a reference to the same object (barring
an override of __copy__).

You could probably view the general deepcopy guideline as making the
minimum number of actual copies to ensure that no references to
mutable objects from the original object remain in the copy, but that
the internal reference structure from the original is maintained. In
general, I find that to be a very practical approach.

Of course, if that's not what you wanted, then deepcopy isn't going to
fit the bill. If you have control of the objects involved, the
__copy__ and __deepcopy__ hooks are provided to let you make some
changes, but it certainly may not fit all cases. But that hardly
makes it "incorrect" or less useful for cases where it does fit (which
to be honest, I'd guess is the majority of them).

-- David
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2...
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...
0
by: phil | last post by:
I wrote the following to prove to myself that deepcopy would copy an entire dictionary which contains an instance of a class to one key of another dictionary. Note that after copying adict to...
6
by: phil | last post by:
I posted the following yesterday and got no response and did some testing simplifying the circumstances and it appears that deepcopy fails when the object to be copied contains a reference to a...
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...
7
by: Alexandre Guimond | last post by:
Hi all, i'm trying to deepcopy a slice object but i get the following error. Does anyone know a workaround? ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on Python 2.4.3 (#69,...
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
by: Robin Becker | last post by:
I'm using deepcopy in some code which eventually ends up by crash witht he following rather long winded error. I'm not directly using _hashlib.HASH, but I suppose something else along the way could...
1
by: Wouter DW | last post by:
I read the article on http://www.python.org/download/releases/2.2/descrintro/#metaclasses and started using autoprop. But now I have a problem I can't seem to solve myself. class autoprop(type):...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.