首页 > 代码库 > 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 温标的转换