Python“延迟计算”编程
这是一个两部分的
python问题.第一部分是关于美学,第二部分是关于实施.我到目前为止工作的代码示例附在底部.
问题. 我正在尝试设计一个python模块/类,使我能够进行延迟评估/延迟计算. 从本质上讲,我希望能够做到尽可能接近这样的事情: >>> a=3 >>> b=5 >>> c=a+b >>> c 8 >>> a = 6 >>> c 11 和之类的东西 >>> c.calculation 'a [ 6 ] + b [ 5 ] = 11' 现在我不是那个关于循环依赖关系的问题,而且我最接近的是: a = constant(2) b = constant(3) c = a + b d = a * b 这给了我这样的东西: c is 5 [ 2 <built-in function __add__> 3 ] d is 6 [ 2 <built-in function __mul__> 3 ] 这并不完美,但它越来越近了.然后我改变a和b的值,以便重新计算c,d的值 我知道我不能使用赋值修改a,b的值而不完全覆盖它,所以像 Q1:美学:最好的方法是什么才能让客户使用它是最明显的(pythonic?)方式? 接下来,如果您查看下面的代码,您将看到我刚刚为add和mul实现了运算符,但我想重现这个 Q2:实施:对我进行编码的最佳方式是什么? 最后,我希望能够使用这个库做一些矩阵产品/总和. 我知道这将是一个记忆力 – 但对我来说,得到“这个数字最终是如何成为x”的解释对我来说比内存使用/ cpu周期更重要. import operator class operable(object): def __add__(self,other): if isinstance(other,operable): d = calculation() d.operator = operator.__add__ d.operands = [self,other] return d def __mul__(self,operable): d = calculation() d.operator = operator.__mul__ d.operands = [self,other] return d class calculation(operable): def __init__(self): self.operands = [] self.operator = None @property def value(self): return reduce(self.operator,[x.value for x in self.operands]) @property def calculation(self): return (" %s " % str(self.operator)).join([x.__repr__() for x in self.operands]) def __repr__(self): return "%d [ %s ] " % ( self.value,self.calculation ) class constant(operable): def __init__(self,x = 0): self._value = x def __repr__(self): return "%d" %( self.value) @property def value(self): return self._value @value.setter def value(self,new_val): self._value = new_val def test_1(): a = constant(2) b = constant(3) c = a + b d = a * b z = a + b + c + d print "c is",c print "d is",d print "z is ",z b.value = 5 print "c is now",c print "d is now",d print "z is now ",z if __name__ == "__main__": test_1() 解决方法
我非常喜欢Raymond Hettinger解决类似问题的方法:
class SpreadSheet: _cells = {} tools = {} def __setitem__(self,key,formula): self._cells[key] = formula def getformula(self,key): return self._cells[key] def __getitem__(self,key ): return eval(self._cells[key],SpreadSheet.tools,self) >>> from math import sin,pi >>> SpreadSheet.tools.update(sin=sin,pi=pi,len=len) >>> ss = SpreadSheet() >>> ss['a1'] = '5' >>> ss['a2'] = 'a1*6' >>> ss['a3'] = 'a2*7' >>> ss['a3'] 210 >>> ss['b1'] = 'sin(pi/4)' >>> ss['b1'] 0.70710678118654746 >>> ss.getformula('b1') 'sin(pi/4)' 取自:http://code.activestate.com/recipes/355045-spreadsheet/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |