问题描述
代码如下:
def my_func(f, arg):
return f(arg)
print((lambda x: 2*x*x, (5)))
>>>(<function <lambda> at 0x10207b9d8>, 5)
如何解决该错误,并且可以请一些人用清晰的语言解释该错误的确切含义.
How to solve the error, and can some please explain in a clear language what exactly that error means.
推荐答案
没有错误.您只需为print
提供了两个参数,分别为lambda x: 2*x*x
和5
.您不是在调用匿名函数,而是将其传递给print
.
There is no error; you simply supplied two arguments to print
, the lambda x: 2*x*x
and 5
. You're not calling your anonymous function rather, just passing it to print
.
print
然后将调用对象__str__
方法,该方法将返回您看到的内容:
print
will then call the objects __str__
method which returns what you see:
>>> str(lambda x: 2*x*x) # similarly for '5'
'<function <lambda> at 0x7fd4ec5f0048>'
相反,将括号修正为实际上调用传递了作为x
值的5
的lambda:
Instead, fix your parenthesis to actually call the lambda with the 5
passed as the value for x
:
print((lambda x: 2*x*x)(5))
会打印出50
.
这就是Python选择打印时表示函数对象的方式.他们的str
带有功能的名称和功能的id
的hex
并打印出来.例如:
That's simply the way Python has chosen to represent function objects when printed. Their str
takes the name of the function and the hex
of the id
of the function and prints it out. E.g:
def foo(): pass
print(foo) # <function foo at 0x7fd4ec5dfe18>
是通过返回字符串创建的:
Is created by returning the string:
"<function {0} at {1}>".format(foo.__name__, hex(id(foo)))
这篇关于<在...上的功能是什么>意思是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!