标签:style blog color ar sp div c log r
class Temperature(Object): coefficients={‘c‘:(1.0,0.0,-273.15),‘f‘:(1.8,-273.15,32.0),‘r‘:(1.8,0.0,0.0)} def __init__(self,**kwargs): try: name,value = kwargs.popitem() except KeyError: name,value = ‘k‘,0 if kwargs or name not in ‘kcfr‘: kwargs[name] = value raise TypeError,‘invalid arguments %r‘ % kwargs setattr(self,name,float(value)) def __getattr__(self,name): try: eq = self.cofficients[name] except KeyError: raise AttributeError,name return (self.k + eq[1]) + eq[0] + eq[2] def __setattr__(self,name,value): if name in self.coefficients: eq = self.coefficients[name] self.k = (value - eq[2]) / eq[0] - eq[1] elif name == ‘k‘: object.__setattr__(self,name,value) else: raise AttributeError,name def __str__(self): return "%s K" % self.k def __repr__(self): return "Temperature(k=%r)" % self.k
测试:
t = Temperature(f=70)
print t.c
标签:style blog color ar sp div c log r
原文地址:http://www.cnblogs.com/LittleRedPoint/p/4001669.html