Is inheritance broken?

Skip Montanaro skip at
Wed Mar 28 22:03:57 CEST 2001

    Jonathan> Ugly and error prone, no?

Well, perhaps there is a better way...  ;-)

How about creating mixins, then composing a set of classes from them:

    class VaryingAnglesMixin:
	def angle(self): return "variable"

    class SameAnglesMixin:
	def angle(self): return "same"

    class NinetyDegreesMixin:
	def angle(self): return "90 degree"

    class VaryingSidesMixin:
	def side(self): return "variable"

    class SameSidesMixin:
	def side(self): return "same"

    class parallelogram(VaryingAnglesMixin, VaryingSidesMixin):

    class rhombus(VaryingAnglesMixin, SameSidesMixin):

    class rectangle(NinetyDegreesMixin, VaryingSidesMixin):

    class square(NinetyDegreesMixin, SameSidesMixin):

    s = square()
    print "squares have", s.angle(), "angles"
    print "squares have", s.side(), "sides"

    r = rhombus()
    print "rhombuses have", r.angle(), "angles"
    print "rhombuses have", r.side(), "sides"


More information about the Python-list mailing list