(I'm sorry for my delayed response -- I've been travelling and not had reliable Internet access.)
On 2011-12-25, Ian Kelly <ian.g.ke...@gmail.com> wrote: > On Thu, Dec 22, 2011 at 1:21 AM, Spencer Pearson ><speeze.pear...@gmail.com> wrote: >> I see a problem with this, though. The intersection of two lines is >> (usually) an object of type Point. Since DrawableLine inherits from >> Line, this means that unless I redefine the "intersect" method in >> DrawableLine, the intersection of two DrawableLines will be a Point >> object, not a DrawablePoint. I don't want to saddle the user with the >> burden of converting every method output into its corresponding >> Drawable subclass, and I don't want to redefine every method to return >> an instance of said subclass. I see no other options if I continue >> down the subclassing path. Am I missing something? > > You could solve this with a factory. Instead of having > Line.intersection() create a Point directly, have it call > self.factory.create_point(). In the drawing module, replace the > factory for the subclasses with one that creates drawable classes > instead. Oh, that's a neat idea. Yes, I think that does exactly what I want! Thanks very much! > This will also work, but inheritance complicates things a > little. ... > ... Probably the easiest way to do this correctly is to follow the > MRO that Python has conveniently already built for you. Something > like: > > def draw(x, *args, **kwargs ): > for class_ in type(x).__mro__: > if class_ in draw_functions: > draw_functions[class_](*args, **kwargs) > break > else: > raise TypeError("don't know how to draw things of type " > "{0}".format(type(x))) You're right, you're right. My implementation was sloppy. I think I'll go with your factory solution, but thanks for the fixed version of draw() -- I've never seen __mro__ before, and it seems to be just the tool for this job! -Spencer -- http://mail.python.org/mailman/listinfo/python-list