31 May 2009 07:27
Challenge supporting custom deepcopy with inheritance
Michael H. Goldwasser <goldwamh <at> slu.edu>
2009-05-31 05:27:26 GMT
2009-05-31 05:27:26 GMT
I've been playing around recently with customizing the deepcopy semantics within an inheritance hierarchy, and run across the following hurdle. Assume that class B inherits from class A, and that class A has legitimately customized its deepcopy semantics (but in a way that is not known to class B). If we want a deepcopy of B to be defined so that relevant state inherited from A is copied as would be done for class A, and with B able to control how to deepcopy the extra state that it introduces. I cannot immediately find a general way to properly implement the deepcopy of B. To make the discussion tangible, I include an outrageously artificial example in the code fragment below. In this code, class A implements __deepcopy__ to intentionally create a clone that has a reversed version of a list instance. The beginning of the main test demonstrates that an instance of A can successfully be cloned with this semantics. But the attempt to deepcopy an instance of B fails (the current failure is because B has not implemented __deepcopy__ and the inherited version of A.__deepcopy__ assumes the wrong constructor signature). The question is how class B should defined so that its instances can "inherit" this deepcopy semantic. Ideally, I'd like to find a recipe for class B that works regardless of how A accomplishes its semantics. That is, I can find ways to rewrite A to be more supportive of B's goal, but I'd like to know if there is a general solution that could be used if the author of B doesn't have control over A's implementation.(Continue reading)
RSS Feed