Classes

Chris Angelico rosuav at gmail.com
Mon Nov 3 16:34:30 CET 2014


On Tue, Nov 4, 2014 at 12:50 AM, Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:
> On Mon, 3 Nov 2014 03:12:32 +0000 (UTC), Denis McMahon
> <denismfmcmahon at gmail.com> declaimed the following:
>
>>So:
>>
>>Quadrilateral
>>    Parallelogram
>>        Square
>>        Rectangle
>>        Rhombus
>>        Diamond (4 sides eq)
>>    Trapezoid
>>        Arrowhead
>>
>>Is an arrowhead a trapezoid?
>
>         I'd probably indent square under rectangle, and indent diamond where it
> is -- both being restrictions on rectangle/rhombus
>
>         But we may need another category, convex and concave; the arrowhead
> would fall into the concave grouping.

That's fine if you're creating a taxonomical hierarchy of immutables,
but neither works if you're creating a mutable class hierarchy that
follows LSP. The only way you can make any of this work (as someone
suggested earlier, I believe) is by having __new__ potentially return
a subclass, so if you ask Parallelogram to give you one with all sides
equal and all angles equal, it gives you back a Square. But since a
square is a special form of rhombus as well as a special form of
rectangle, you'd have to use multiple inheritance or an AST-style
subclass hook to verify. Of course, if you're going down that route,
you probably want to take this up a level and have a Shape class, and
have the subclass hook simply check if it's four-sided as well.

ChrisA



More information about the Python-list mailing list