使用eval创建匿名函数 [英] Creating anonymous functions using eval

查看:79
本文介绍了使用eval创建匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩一个创建匿名函数的函数,通过

编译一个字符串参数,它看起来效果很好:


def fn(text):

exec''def foo''+ text.strip()

return foo


这可以用作:


def foo(x):

打印x(2,5)


foo(fn(''''''

(x,y):

print''x ^ 2 + y ^ 2 ='',x * x + y * y

返回y

'''''')


- 输出:


x ^ 2 + y ^ 2 = 29

5

您还可以模仿传统的功能定义:


f = fn(''''''

(x,y):

print''x,y ='',x,y
打印1.0 * x / y

返回y

''''''


print' '打电话''

f(5,6)


输出:


呼叫f

x,y = 5 6

0.833333333333

你做的事情就像允许/要求初始'def''一样清楚,检查所有

的行文本是缩进的,以便在创建匿名

函数等时不执行任何操作。


明显的缺点是函数文本可能必须转义引号,

并且不会在编辑器中显示语法着色。


除此之外,它似乎是一种克服

python'的lambda。


但是......我没有看到过任何地方提到的这种技术,所以有人都知道

我应该警惕的任何问题吗?


- Julian


-
http://www.op59.net/

I''ve been playing with a function that creates an anonymous function by
compiling a string parameter, and it seems to work pretty well:

def fn( text):
exec ''def foo'' + text.strip()
return foo

This can be used like:

def foo( x):
print x( 2, 5)

foo( fn( ''''''
( x, y):
print ''x^2 + y^2 = '', x*x + y*y
return y
''''''))

- which outputs:

x^2 + y^2 = 29
5
You can also mimic conventional function definitions:

f = fn( ''''''
( x, y):
print ''x, y='', x, y
print 1.0*x/y
return y
'''''')

print ''calling f''
f( 5, 6)

This outputs:

calling f
x, y= 5 6
0.833333333333
You do things like allow/require an initial `def'' for clarity, check that all
the lines of text are indented so that nothing is executed when the anonymous
function is created, etc etc.

Obvious disadvantages are that the function text may have to escape quotes,
and will not be showed with syntax colouring in editors.

Apart from that, it seems quite a useful way of overcoming the limitations of
python''s lambda.

But... I haven''t seen this technique mentioned anywhere, so does anyone know
of any problems that I should be wary of?

- Julian

--
http://www.op59.net/

推荐答案

这与嵌套函数有何不同?

How is this different from a nested function?


7月12日星期二2005 08:28:45 -0700,Devan L写道:
On Tue, 12 Jul 2005 08:28:45 -0700, Devan L wrote:
这与嵌套函数有什么不同?
How is this different from a nested function?




好吧,这个是由您撰写的新闻组发布。嵌套函数在

Python中是可调用的对象,作为其他可调用的

对象的属性存在,所以这两者是非常不同的。


或者,如果你期待一个明智的答案,那么问一个明智的问题呢?你可以先告诉我们你用b $ b来比较嵌套函数是什么。


-

史蒂文。



Well, "this" is a newsgroup posting written by you. Nested functions in
Python are callable objects that exist as attributes of other callable
objects, so the two are very different.

Alternatively, if you expect a sensible answer, how about asking a
sensible question? You can start by telling us just what it is that you
are comparing to nested functions.

--
Steven.


好吧,传递的字符串或多或少是一个函数

definition,然后用exec调用。我不明白你为什么需要

用函数定义写出一个字符串然后调用它。

你可以写这个函数。 br />

至于嵌套函数,我一直认为用作函数中lambda的更好函数是

。抱歉

混乱。

Well, the string that gets passed is more or less a function
definition, which is then called with exec. I don''t see why you''d need
to write a string out with the function definition and then call it.
You could just write the function.

As for the nested functions, I had been presuming that it was intended
to use as a better function than lambda within a function. Sorry for
the confusion.


这篇关于使用eval创建匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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