The class Event implements a function copyFrom(self, event) and copy(self). The implementation of both methods is simple made cake.
class Event(object):
def __init__(self, a, b):
self.a = a
self.b = b
def copyFrom(self, event):
self.a = event.a
self.b = event.b
def copy(self):
return Event(self.a, self.b)
Now, the subclass MouseEvent wants to override both of them. The first method, copyFrom(self, event) can be implemented by calling the super-method.
class MouseEvent(Event):
def __init__(self, a, b, c, d):
super(Event, self).__init__(a, b)
self.c = c
self.d = d
def copyFrom(self, event):
super(Event, self).copyFrom(event)
self.c = event.c
self.d = event.d
But what about copy(self) ? Of course, just creating a new object is not hard.
# ...
def copy(self):
return MouseEvent(self.a, self.b, self.c, self.d)
But what if the Event class owns some private attributes ? The person that wants to subclass it, does not want to care about them !
class Event(object):
def __init__(self, a, b):
self.a = a
self.b = b
self._aab = fancyFunction(a, b)
def doStuff(self, c):
self._aab <<= c * 2
def copyFrom(self, event):
self.a = event.a
self.b = event.b
self._aab = event._aab
def copy(self):
e = Event(self.a, self.b)
e._aab = self._aab
return e
This implementation of copy(self) looks quite dirty now, and so does the overriden one from MouseEvent.
class MouseEvent(Event):
# ...
def copy(self):
e = MouseEvent(self.a, self.b, self.c, self.d)
e._aab = self._aab
return e
How can I implement an easy, maybe recursive, copy-behaviour in this particular example ?
First, isn’t it enough that the
MouseEventconstructor recursively calls theEventconstructor?If not, have a look at the
copymodule. It is based onpickle‘s state retrieval and restoration protocol. Using this protocol, recursive calls are easy. You could of course just copy this basic design if you don’t want to conform to thepickleinterface, by I don’t see any advantage to doing so.Edit: It seems I failed to get across the message, and indeed my answer might be a bit vague. So here we go with a code example using the suggested design:
Note that you wouldn’t even need the
copy()andcopy_from()methods if you would simply renameretrieve_state()andrestore_state()to__getstate__()and__setstate__(), respectively, because after toding this you can just use the standardcopy()module. (And in the case at hand, you wouldn’t even need to define__getstate__()and__setstate__()at all, since the default behaviour is to save and restore all instance attributes.)