Re: Semi-newbie, rolling my own __deepcopy__



Steven Bethard wrote:
Michael Spencer wrote:

    def __deepcopy__(self, memo={}):
        from copy import deepcopy
        result = self.__class__()
        memo[id(self)] = result
        result.__init__(deepcopy(tuple(self), memo))
        return result


I know this is not your recipe, but is there any reason to use
    self.__class__()
instead of
    type(self)()
if you know you're inside a new-style class?

STeVe
I don't know - aren't they identical? I would write self.__class__ (without claiming that that's better)

BTW, I had a different question about the method:

wouldn't:
    result = self.__class__.__new__()
or in your form:
    result = type(self).__new__()

be better (i.e., clearer and possibly safer) than calling __init__ twice (but I haven't tried it!)

Michael



.



Relevant Pages

  • Re: scared about refrences...
    ... See my second example post that used deepcopy... ... Did it close your python window without a word? ... I know it seems unnecessary to post the traceback when I could get the ... state = deepcopy(state, memo) ...
    (comp.lang.python)
  • Re: scared about refrences...
    ... functions should not modify their caller's data. ... See my second example post that used deepcopy... ... When it blows up, is there a lot of shrapnel, or just smoke and fire? ... state = deepcopy(state, memo) ...
    (comp.lang.python)
  • Re: Operator += works once, fails if called again
    ... It lists days and ... File "C:\PYTHON24\lib\copy.py", line 188, in deepcopy ... y = _reconstruct(x, rv, 1, memo) ... Some species of contained Lists need a reference to the containing Time_Series and that circular reference trips up deepcopy. ...
    (comp.lang.python)
  • Re: scared about refrences...
    ... See my second example post that used deepcopy... ... While a traceback might look like a big mess at first, ... state = deepcopy(state, memo) ... Looks like a bug in the array module to me. ...
    (comp.lang.python)
  • deepcopy debugging
    ... I'm using deepcopy in some code which eventually ends up by crash witht he following rather long winded error. ... state = deepcopy(state, memo) ...
    (comp.lang.python)