Michael Van Biesbrouck added the comment: 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 semantics.
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. __________________________________ Tracker <[EMAIL PROTECTED]> <http://bugs.python.org/issue1515> __________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com