"更新" lambda函数 [英] "Updating" lambda functions

查看:60
本文介绍了"更新" lambda函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试编写一个解析

表达式并从中构建函数树的Python函数

(递归)。


在解析过程中,术语

和子表达式的lambda函数是动态构建的。

现在我的问题是懒惰的评价。或者至少我认为它是b $ b。 :-)


我需要更新一个lambda函数,像这样:


fu = lambda x:x

...

fu = lambda x:fu (x)+ 17

...

fu = lambda x:fu(x)* 3


当然是不起作用,因为fu被解决了,当调用lambda时,
,而不是在定义时,所以

我会遇到无休止的递归。


我目前的解决方案是定义一个辅助函数

,它通过它的参数传递lambda:


def add_17(fu):

返回lambda x:fu(x)+ 17

def mul_3(fu):

return lambda x:fu(x)* 3

fu = lambda x:x

...

fu = add_17 (fu)

...

fu = mul_3(fu)


这样可行,但它让我觉得不洁净丑陋。

还有更好的办法吗?


祝你好运

奥利弗


-

Oliver Fromme,Konrad-Celtis-Str。 72,81369慕尼黑,德国


`所有我们看到或看起来只是梦中的梦想。'''

(EA Poe)

Hi,

I''m trying to write a Python function that parses
an expression and builds a function tree from it
(recursively).

During parsing, lambda functions for the the terms
and sub-expressions are constructed on the fly.
Now my problem is lazy evaluation. Or at least I
think it is. :-)

I need to "update" a lambda function, like this:

fu = lambda x: x
...
fu = lambda x: fu(x) + 17
...
fu = lambda x: fu(x) * 3

Of course that doesn''t work, because fu is resolved
when the lambda is called, not when it''s defined, so
I''ll run into an endless recursion.

My current solution is to define a helper function
which passes the lambda through its argument:

def add_17 (fu):
return lambda x: fu(x) + 17

def mul_3 (fu):
return lambda x: fu(x) * 3

fu = lambda x: x
...
fu = add_17(fu)
...
fu = mul_3(fu)

That works, but it strikes me as unclean and ugly.
Is there a better way to do it?

Best regards
Oliver

--
Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

``All that we see or seem is just a dream within a dream.''''
(E. A. Poe)

推荐答案

2004年9月16日星期四14:07:20 +0000,Oliver Fromme写道:
On Thu, 16 Sep 2004 14:07:20 +0000, Oliver Fromme wrote:
我需要[更新"一个lambda函数,如下所示:
I need to "update" a lambda function, like this:




这表示你需要使用类,而不是lambda函数。



That says you need to use classes, not lambda functions.


Oliver Fromme <醇** @ haluter.fromme.com>在消息中写道

新闻:2q ************* @ uni-berlin.de ...


|这很有效,但它让我感到不洁和丑陋。

|有没有更好的方法呢?


我也不喜欢它。考虑一下:
"Oliver Fromme" <ol**@haluter.fromme.com> wrote in message
news:2q*************@uni-berlin.de...

| That works, but it strikes me as unclean and ugly.
| Is there a better way to do it?

I don''t like it either. Consider this:
composition = lambda f,g:lambda x:f(g(x))
add_17 = lambda x:x + 17
mul_3 = lambda x:3 * x
fu = lambda x:x
fu(3)
3 fu = composition(add_17 ,fu)
fu(3)
20 fu =作曲(mul_3,fu)
fu(3)
composition = lambda f,g: lambda x: f(g(x))
add_17 = lambda x: x+17
mul_3 = lambda x: 3*x
fu = lambda x:x
fu(3) 3 fu = composition(add_17,fu)
fu(3) 20 fu = composition(mul_3,fu)
fu(3)



60


60


2004年9月16日14:07:20 GMT,Oliver Fromme< ol ** @ haluter.fromme.com>写道:
On 16 Sep 2004 14:07:20 GMT, Oliver Fromme <ol**@haluter.fromme.com> wrote:


我正在尝试编写一个Python函数来解析表达式并从中构建一个函数树
(递归地)。

在解析过程中,术语
和子表达式的lambda函数是动态构建的。
现在我的问题是懒惰的评估。或者至少我认为它是。 :-)

我需要更新一个lambda函数,像这样:

fu = lambda x:x
...
fu = lambda x:fu(x)+ 17
...
fu = lambda x:fu(x)* 3

当然这不起作用,因为在调用lambda时fu已经解决了,而不是在它被调用时已定义,所以
我会遇到无休止的递归。

我目前的解决方案是定义一个辅助函数
,它通过它的参数传递lambda:

def add_17(fu):
返回lambda x:fu(x)+ 17
def mul_3(fu):
返回lambda x:fu( x)* 3
fu = lambda x:x
...
fu = add_17(fu)
...
fu = mul_3( fu)

这样可行,但它让我感到不洁和丑陋。
有没有更好的方法呢?

最好的问候
奥利弗

-
Oliver Fromme,Konrad-Celtis-Str。 72,81369慕尼黑,德国

``我们看到或看起来只是梦中的梦想。'''
(EA Poe)
Hi,

I''m trying to write a Python function that parses
an expression and builds a function tree from it
(recursively).

During parsing, lambda functions for the the terms
and sub-expressions are constructed on the fly.
Now my problem is lazy evaluation. Or at least I
think it is. :-)

I need to "update" a lambda function, like this:

fu = lambda x: x
...
fu = lambda x: fu(x) + 17
...
fu = lambda x: fu(x) * 3

Of course that doesn''t work, because fu is resolved
when the lambda is called, not when it''s defined, so
I''ll run into an endless recursion.

My current solution is to define a helper function
which passes the lambda through its argument:

def add_17 (fu):
return lambda x: fu(x) + 17

def mul_3 (fu):
return lambda x: fu(x) * 3

fu = lambda x: x
...
fu = add_17(fu)
...
fu = mul_3(fu)

That works, but it strikes me as unclean and ugly.
Is there a better way to do it?

Best regards
Oliver

--
Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

``All that we see or seem is just a dream within a dream.''''
(E. A. Poe)




你可以利用函数成为绑定方法的方式,例如,



You could exploit the way functions become bound methods, e.g.,

fu = lambda x:x
fu =(lambda f,x:f(x)+ 17).__ get __(fu)
fu =(lambda f,x:f(x)* 3).__ get __( fu)
fu(1)
54 fu(0)
51 fu(-15)
6 fu(-16)
3 fu(1,2)
Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:< lambda>( )取2个参数(3个给定)fu
< bound method?。< lambda> < bound方法?< lambda> < function< lambda>在0x008FD8B0>>>


或者,你可以使用魔法组合属性创建一个神奇的函数组合对象,

利用方法制作机制另一种方式,例如,

类FC(对象):
... def __setattr __(self,name,f):

......如果没有hasattr(self,name):self .__ dict __ [name] = [f]

... else:self .__ dict __ [name] .append(f)

。 .. def __getattribute __(self,name):

...如果name ==''__ dict__'':return object .__ getattribute __(self,''__ dict__'')

...返回类型(self).__ dict __ [''_ xfuns''] .__ get __(

... object .__ getattribute __(self,name))

... def _xfuns(flist,x):

... for f in flist:x = f(x)

...返回x

... fc = FC()
fc.fu = lambda x:x
fc.fu = lambda x:x + 17
fc.fu = lambda x:x * 3
fc.fu(1)
54 fc.fu(0)
51 fc.fu(-15)
6 fc.fu(1,2)
Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:_xfuns()需要2个参数(给定3个)fc.bar = lambda x:x * 3
fc.bar = lambda x:x + 10
fc.bar(0)
10 fc.bar(2)
16 fc.fu(-18)
fu = lambda x: x
fu = (lambda f,x: f(x) + 17).__get__(fu)
fu = (lambda f,x: f(x) * 3).__get__(fu)
fu(1) 54 fu(0) 51 fu(-15) 6 fu(-16) 3 fu(1,2) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: <lambda>() takes exactly 2 arguments (3 given) fu <bound method ?.<lambda> of <bound method ?.<lambda> of <function <lambda> at 0x008FD8B0>>>

or, you could make a magic function-composing object with magic composing properties,
exploiting the method-making mechanism in a different way, e.g.,
class FC(object): ... def __setattr__(self, name, f):
... if not hasattr(self, name): self.__dict__[name] = [f]
... else: self.__dict__[name].append(f)
... def __getattribute__(self, name):
... if name == ''__dict__'': return object.__getattribute__(self, ''__dict__'')
... return type(self).__dict__[''_xfuns''].__get__(
... object.__getattribute__(self,name))
... def _xfuns(flist, x):
... for f in flist: x = f(x)
... return x
... fc = FC()
fc.fu = lambda x: x
fc.fu = lambda x: x + 17
fc.fu = lambda x: x * 3
fc.fu(1) 54 fc.fu(0) 51 fc.fu(-15) 6 fc.fu(1,2) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: _xfuns() takes exactly 2 arguments (3 given) fc.bar = lambda x: x*3
fc.bar = lambda x: x+10
fc.bar(0) 10 fc.bar(2) 16 fc.fu(-18)



-3


所有这些只是为了探索python的功能,而不是推荐特定用途;-)


问候,

Bengt Richter


-3

All just to explore python''s features, not to recommend specific uses ;-)

Regards,
Bengt Richter


这篇关于&QUOT;更新&QUOT; lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆