gpt4 book ai didi

Python "deferred calculation"编程

转载 作者:太空宇宙 更新时间:2023-11-03 11:53:50 24 4
gpt4 key购买 nike

这是一个由两部分组成的 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 的值而不完全覆盖它,所以像 a = 2, b = 3 就出来了。

问题 1:美学:最好的编写方式是什么,以便它成为客户使用的最明显(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,other):
if isinstance(other,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/

关于Python "deferred calculation"编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18784804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com