[Edu-sig] Uniform Access Principle confusion

Kirby Urner urnerk at qwest.net
Fri Oct 14 03:32:36 CEST 2005


> >  >>> mytri = Triangle()
> >  >>> mytri.C.degrees
> >  90
> >  >>> mytri.C.radians
> >  1.5707963267948966
> 
> Well that looks simple, elegant, and straightforward enough for my
> purposes.
> 
> 
> Is there a simple elegant and straightforward way to implement this API in
> Python that I am missing?  I know better than to assume that I don't miss
> the obvious when it comes to this stuff.
> 
> Art
> 
> 

 >>> class Angle (object):

	def __init__(self, thevalue):
		self.value = float(thevalue)

	@property
	def degrees(self):
		return self.value

	@property
	def radians(self):
		return math.radians(self.value)

 >>> class Triangle (object):

	def __init__(self, A=30, B=60, C=90):
		self.A = Angle(A)
		self.B = Angle(B)
		self.C = Angle(C)

		

	
 >>> mytri = Triangle()
 >>> mytri.C.degrees
 90.0
 >>> mytri.C.radians
 1.5707963267948966
 >>> mytri.A.degrees
 30.0
 >>> mytri.A.radians
 0.52359877559829882

Kirby




More information about the Edu-sig mailing list