I have some Python classes that, if simplified, look like:
class base: def __init__(self, v): self.value = v def doThings(self): print "Doing things." def doMoreThings(self): print "Doing more things." def combine(self, b): self.value += b.value class foo(base): def showValue(self): print "foo value is %d." % self.value class bar(base): def showValue(self): print "bar value is %d." % self.value The base class contains methods (represented above by doThings and doMoreThings) which implement functionality common to both the foo and bar subclasses. The foo and bar subclasses differ essentially in how they interpret the value field. (Above, they only differ by what they show when printing it, but in my actual application, they do several other things which are more complicated.) base can be thought of as "abstract": users only ever work with foos and bars. base exists only as a home for code common to its subclasses.
The method I want to ask about is combine, which lets you take two of these objects and make a third. Because foo and bar interpret value differently, it doesn't make sense to combine two subclasses of different types: you can combine two foos to get a foo or two bars to get a bar but not a foo and a bar. Even so, the procedure for combine is the same for all subclasses, so it makes sense to have it factored out and defined in one place.
I would probably like to signal an error if a user tries to combine two incompatible objects, but I don't see a way to do this without introducing ugly typechecks. Is it good practice to do this? Or should I do the usual thing and not check, document the issue, and assume that the user won't try to use combine in a way that wasn't intended, even though such use would appear to "succeed" and return a garbage object instead of raising an error?
Thank you for your help.