python中延迟函数_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 3 ]

d is 6 [ 2 3 ]

这并不完美,但它越来越近了.然后我改变a和b的值,以便重新计算c,d的值

我知道我不能使用赋值修改a,b的值而不完全覆盖它,所以像

a = 2,b = 3就出来了.

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,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)'

标签:python,metaprogramming,deferred-execution

来源: https://codeday.me/bug/20190709/1408185.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值