[Edu-sig] Design Patterns

Kirby Urner urnerk at qwest.net
Thu Aug 25 18:45:42 CEST 2005


Hi Andre --

If you scroll back, you'll see I was specifying a triangle object in which
sides a,b,c were passed to a constructor, *and* could be respecified on the
fly, thereby changing the triangle.  A validator made sure the proposed
triangle was legal (triangle inequality not violated).  Other than that,
just go ahead and respecify a,b,c at will:

>>> ok = Triange(3,4,5)
>>> ok  # whatever __repr__
<Triangle <3-4-5>>  
>>> ok.b = 3
>>> ok
<Triangle <3-3-5>>

So it went from right to isosceles just there, thanks to rebinding of side
b.  

However, *only* sides could be respecified in this way.  That was the game.
Think of it as an abcTriangle.  This design has its limitations and
shouldn't always be used.  It's handy when that's what you want.

Angles A,B,C and xyz coordinates pA, pB, pC turned out to be consequent
read-only values, but I decided to make them work like attributes from the
user's point of view.  You could get them, but not set them (directly).

Down the road, I might add scale(), rotate() and translate() methods.  These
would be callables and expect arguments -- except I've already implemented
scale() a different way, using __mul__:

>>> newok = ok * 3
>>> newok
<Triangle 9-9-15>


Kirby




More information about the Edu-sig mailing list