Author mlvanbie
Recipients Dmitrey, christian.heimes, gvanrossum, mlvanbie
Date 2008-01-31.22:54:02
SpamBayes Score 5.64885e-05
Marked as misclassified No
Message-id <>
Dmitrey: You can't call _deepcopy_method() on anything other than
something with type types.MethodType.  It is a function in a
type-dispatch table, so it will always be called safely. 
copy._deepcopy_dispatch is that table; if you assign _deepcopy_method to
copy._deepcopy_dispatch[types.MethodType] then copy.deepcopy() will
understand what to do with method types.  See my unit test below for the

Christian Heimes, this is my unit test:

# This test would fail due to an exception during deepcopy in version 2.5.1

class C(object):
  def __init__(self, n):
    self.n = n

  def GetN(self):
    return self.n

orig_obj = C(42)
copy_list = copy.deepcopy([orig_obj, orig_obj.GetN])
if copy_list[1]() != 42:
  print 'Error: Instance method lost object?'
orig_obj.n = 43
if copy_list[1]() != 42:
  print 'Error: Instance method assoc with orig object'
copy_list[0].n = 44
if copy_list[1]() != 44:
  print 'Error: Instance method should assoc with new object copy'
if not type(copy_list[1]) is type(orig_obj.GetN):
  print 'Error: Deepcopy changed type of instance method'

Why do people want to use copy and deepcopy?  I think that the issue is
that Python is an imperative language that passes and copies references.
 If a library function doesn't treat its arguments as const, horrible
things could happen.  Compounding this, several standard operations
(such as sort) operate in place, so if you want to use them then you
need to make shallow copies at the very least.  When non-scalar types
nest, the whole structure can be deepcopied or copy can be used
repeatedly in a selective manner (with a high chance of bugs).  Perl
deals with this issue by using standard copy semantics (but
call-by-reference) and a copy-on-write implementation.  Most operations
operate out-of-place.  Pure languages can use reference-based copy
semantics because you can't modify anything, forcing users to create the
copy-on-write implementation when mutating structures.
Date User Action Args
2008-01-31 22:54:07mlvanbiesetspambayes_score: 5.64885e-05 -> 5.64885e-05
recipients: + mlvanbie, gvanrossum, christian.heimes, Dmitrey
2008-01-31 22:54:07mlvanbiesetspambayes_score: 5.64885e-05 -> 5.64885e-05
messageid: <>
2008-01-31 22:54:03mlvanbielinkissue1515 messages
2008-01-31 22:54:02mlvanbiecreate