scared about refrences...

SpreadTooThin bjobrien62 at gmail.com
Mon Oct 30 19:23:11 EST 2006


J. Clifford Dyer wrote:
> SpreadTooThin wrote:
> > 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...
> >
> When it blows up, is there a lot of shrapnel, or just smoke and fire?
> Is the shrapnel mostly metal, or is it plastic and glass?
>
> In short, if we don't know what's happening, we can't help.
> * Did the program spit out a bunch of text you didn't understand?
>    If so, show us the text.  That text may be incomprehensible at first,
>    but it contains crucial clues.
>
> * Did it close your python window without a word?
>    Tell us.
>
> * Did your computer freeze up?
>    Tell us.
>
> If you don't tell us what went wrong *exactly*, you won't get a
> satisfactory answer.
>

I would assume that looking at the code you should be able to tell..
Silly me..  Here.. is the log.. If I were helping.. I would have cut
and pasted the code myself and ran it.. instead of trying to interpret
this...

array('H', [1, 2, 3]) ['a', 'b', 'c']
Traceback (most recent call last):
  File
"/Volumes/Data/Users/bjobrien/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py",
line 1806, in runMain
    self.dbg.runfile(debug_args[0], debug_args)
  File
"/Volumes/Data/Users/bjobrien/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py",
line 1529, in runfile
    h_execfile(file, args, module=main, tracer=self)
  File
"/Volumes/Data/Users/bjobrien/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py",
line 590, in __init__
    execfile(file, globals, locals)
  File "/Volumes/Data/Users/bjobrien/Desktop/pythonDICOM/Text-1.py",
line 20, in __main__
    test(t)
  File "/Volumes/Data/Users/bjobrien/Desktop/pythonDICOM/Text-1.py",
line 16, in test
    t = copy.deepcopy(x)
  File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
line 174, in deepcopy
    y = copier(x, memo)
  File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
line 305, in _deepcopy_inst
    state = deepcopy(state, memo)
  File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
line 174, in deepcopy
    y = copier(x, memo)
  File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
line 268, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
line 185, in deepcopy
    y = copier(x, memo)
TypeError: __deepcopy__() takes no arguments (1 given)



> Cheers,
> Cliff




More information about the Python-list mailing list