Is inheritance broken?

Skip Montanaro skip at pobox.com
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):
	pass

    class rhombus(VaryingAnglesMixin, SameSidesMixin):
	pass

    class rectangle(NinetyDegreesMixin, VaryingSidesMixin):
	pass

    class square(NinetyDegreesMixin, SameSidesMixin):
	pass

    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"

Skip




More information about the Python-list mailing list