首页 > 代码库 > cookbook 6.1 温标的转换
cookbook 6.1 温标的转换
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
cookbook 6.1 温标的转换
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。