Adding an interface to existing classes

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Dec 25 07:10:40 EST 2011


On Sat, 24 Dec 2011 17:24:14 -0800, Rick Johnson wrote:

>> class DrawablePoint( geometry.Point ):
>>     class draw( self, ... ):
>>         ...
> 
> "DrawablePoint"? I suppose there is an "ImaginaryPoint" somewhere in
> this API? Geez

No, but there's an abstract Point, which presumably refers to the 
geometric concept (hence the module, geometry) without concerning itself 
with such things as pixels, raster and vector output devices, screen 
resolutions, and all the other stuff which is needed for drawing points 
but not needed for working with points.


[...]
>> I see a problem with this, though. The intersection of two lines is
>> (usually) an object of type Point. Since DrawableLine inherits from
>> Line,
> 
> Why the hell is "Drawable" inheriting from Line? I would think that a
> "Line" would be "Drawable" object and NOT vice-versa? Am i wrong?

Probably. I think there's a case for Drawable to be an abstract mixin 
class, so that DrawableLine inherits from both Line and Drawable.


>> this means that unless I redefine the "intersect" method in
>> DrawableLine, the intersection of two DrawableLines will be a Point
>> object, not a DrawablePoint.

Not if you define intersect in Point correctly in the first place.

class Point:  # An abstract class.
    def intersect(self, other):
        blah; blah; blah
        return Point(x, y)  # No, wrong, bad!!! Don't do this.

Instead:

        return self.__class__(x, y)  # Better.


> Spencer, i would re-think this entire project from the beginning. You
> are trying to make an object out of everything. You don't need to make
> an object of EVERYTHING.

Very true.



-- 
Steven



More information about the Python-list mailing list