Re: scared about refrences...




Steven D'Aprano wrote:
On Mon, 30 Oct 2006 13:10:47 -0800, SpreadTooThin wrote:

How do I specify or create deep copies of objects that may contain
other objects that may contain other object that may contain other
objects....

See the `copy` module especially `copy.deepcopy()`.


This appears to be the right thing to do to me.. (but what do I know?)

Yes, copy.deepcopy() is the thing you want.

But remember Fredrik's advice that well-designed Python code should not
need to copy data structures often. I don't think I've ever needed to use
deepcopy, and rarely copy.copy().

In general, functions should not modify their caller's data. So this is
bad practice:

def print_list(alist):
"""Print a sorted list"""
alist.sort() # modifies the caller's data -- bad!
for index, value in enumerate:
print "Value %s at index %d" % (index, value)

This is better:

def print_list(alist):
"""Print a sorted list"""
alist = alist[:] # makes a local shallow copy of the list
alist.sort() # safe to modify now
for index, value in enumerate:
print "Value %s at index %d" % (index, value)

But notice that you only need a shallow copy, not a deep copy, because you
aren't modifying the objects within the list, only the list itself.



I tried this which more closely resembles my project but this doesn't
work:

Unfortunately my crystal ball is back at the shop being repaired, so
you'll have to explain what "doesn't work" means in this case. Does it
raise an exception? If so, please post the exception. Does it do something
different from what you expected? Then what did you expect, and what did
it do?

I seems that some of the objects in the list don't get along well with
deep copy..
See my second example post that used deepcopy... When run blows up...






--
Steven.

.



Relevant Pages

  • Re: scared about refrences...
    ... functions should not modify their caller's data. ... But notice that you only need a shallow copy, not a deep copy, because you ... If so, please post the exception. ... When it blows up, is there a lot of shrapnel, or just smoke and fire? ...
    (comp.lang.python)
  • Re: scared about refrences...
    ... functions should not modify their caller's data. ... def print_list: ... for index, value in enumerate: ... If so, please post the exception. ...
    (comp.lang.python)
  • Re: Script to convert the group type from Domain local to Universal
    ... you modify the groupType attribute of the group objects. ... ' Enumerate all groups in this container and sub containers. ...
    (microsoft.public.windows.server.active_directory)
  • Re: Listing Group Members
    ... This is a script I use to enumerate the groups in my OU. ... able to modify it to suit your own purposes. ... Dim objMember, iCount ... wscript.Echo vbtab & objMember.sAMAccountName & vbtab & ...
    (microsoft.public.scripting.vbscript)
  • Re: Plural of "still life"
    ... we don't modify the "f" in phrases like ... then, that one would indeed modify the "f" in red-roofed, if one said ... An exception to a general rule, indeed, but it's indubitably ...
    (alt.usage.english)