Adding an interface to existing classes

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Dec 25 08:46:15 EST 2011


On Mon, 26 Dec 2011 00:37:22 +1100, Chris Angelico wrote:

> On Mon, Dec 26, 2011 at 12:27 AM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> There's nothing in the above that assumes that other has the same type
>> as self. It's just that the type of other is ignored, and the type of
>> self always wins. I find that a nice, clear rule: x.intersection(y)
>> always returns a point with the same type as x.
> 
> The intersection of DrawableLine and DrawableLine is DrawablePoint.
> That's not the same type as either of the inputs. Same if you seek the
> intersection of two planes, which is a line - or two spheres, which is a
> circle (with possible failure if they don't intersect).

class Line:
    intersection_kind = Point
    def intersection(self, other):
        blah()
        return self.intersection_kind(a, b)

class DrawableLine(Line):
    intersection_kind = DrawablePoint




-- 
Steven



More information about the Python-list mailing list