如何在没有lambda的情况下动态创建函数? [英] How do I dynamically create functions without lambda?

查看:104
本文介绍了如何在没有lambda的情况下动态创建函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的代码符合Python 3000标准,并且听到

正在消除lambda。问题是我想要用一个值来部分绑定一个现有的函数

" foo"直到运行时才知道:


someobject.newfunc = lambda x:f(foo,x)


原因一个嵌套的函数对于它来说不起作用

它是动态的。我不知道多少次

或者foo'会这样做。


现在,我确定有一半 - 打破这样做的方法。

我只想要一种新的,有光泽的Pythonic方式。 ;-)

解决方案

Russell写道:

我希望我的代码符合Python 3000标准,并且听说lambda正在被淘汰。问题是我想要将现有函数与值
foo部分绑定。直到运行时才知道:

someobject.newfunc = lambda x:f(foo,x)

嵌套函数不起作用的原因对于它来说,这是动态的。我不知道有多少次
或foo'将会这样做。

现在,我确信有六种方法可以做到这一点。
我只想要一种新的,有光泽的Pythonic方式。 ; - )




它还不存在。 Python 3000甚至还没有进入规划阶段。只有一些松散的想法浮出水面,围绕着什么(并且不会!)在其中。


你不能写Python现在有3000个兼容代码,因为没有什么要符合

符合。


-

Robert Kern
< a href =mailto:ro ********* @ gmail.com> ro ********* @ gmail.com


"在草地长得高的地狱里

梦想的坟墓是否已经死亡。

- Richard Harter


" Russell" < RU ************ @ hotmail.com>写道:

我希望我的代码符合Python 3000标准,并听到lambda正在被淘汰。




没有人知道但是Python 3000会改变什么,所以放松吧。




Russell写道:

我希望我的代码是符合Python 3000,并且听到lambda正在被淘汰。问题是我想要将现有函数与值
foo部分绑定。直到运行时才知道:

someobject.newfunc = lambda x:f(foo,x)

嵌套函数不起作用的原因对于它来说,这是动态的。我不知道有多少次
或foo'将会这样做。

现在,我确信有六种方法可以做到这一点。
我只想要一种新的,有光泽的Pythonic方式。 ; - )




如果你想编写没有lambda的部分应用程序,我推荐

使用接受的PEP 309中提供的代码

在Python 2.5的功能模块中实现。

http://www.python.org/peps/pep-0309.html




I want my code to be Python 3000 compliant, and hear
that lambda is being eliminated. The problem is that I
want to partially bind an existing function with a value
"foo" that isn''t known until run-time:

someobject.newfunc = lambda x: f(foo, x)

The reason a nested function doesn''t work for this is
that it is, well, dynamic. I don''t know how many times
or with what foo''s this will be done.

Now, I am sure there are a half-dozen ways to do this.
I just want the one, new and shiny, Pythonic way. ;-)

解决方案

Russell wrote:

I want my code to be Python 3000 compliant, and hear
that lambda is being eliminated. The problem is that I
want to partially bind an existing function with a value
"foo" that isn''t known until run-time:

someobject.newfunc = lambda x: f(foo, x)

The reason a nested function doesn''t work for this is
that it is, well, dynamic. I don''t know how many times
or with what foo''s this will be done.

Now, I am sure there are a half-dozen ways to do this.
I just want the one, new and shiny, Pythonic way. ;-)



It doesn''t exist, yet. Python 3000 isn''t even in planning stages, yet. There are
just some loose ideas floating around about what will (and won''t!) be in it.

You can''t write Python 3000 compliant code right now because there is nothing to
comply with.

--
Robert Kern
ro*********@gmail.com

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter


"Russell" <ru************@hotmail.com> writes:

I want my code to be Python 3000 compliant, and hear
that lambda is being eliminated.



Nobody knows yet what Python 3000 will change, so relax.



Russell wrote:

I want my code to be Python 3000 compliant, and hear
that lambda is being eliminated. The problem is that I
want to partially bind an existing function with a value
"foo" that isn''t known until run-time:

someobject.newfunc = lambda x: f(foo, x)

The reason a nested function doesn''t work for this is
that it is, well, dynamic. I don''t know how many times
or with what foo''s this will be done.

Now, I am sure there are a half-dozen ways to do this.
I just want the one, new and shiny, Pythonic way. ;-)



If you want to code partial application without lambda I recommend
using the code presented in the accepted PEP 309 that will be
implemented in the functional module in Python 2.5.

http://www.python.org/peps/pep-0309.html

Kay


这篇关于如何在没有lambda的情况下动态创建函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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