<在...上的功能是什么>意思是 [英] What does <function at ...> mean

查看:69
本文介绍了<在...上的功能是什么>意思是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:

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*x5.您不是在调用匿名函数,而是将其传递给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.

<function at 0x ...>的一般含义是什么?

What's the general meaning of <function at 0x ...>?

这就是Python选择打印时表示函数对象的方式.他们的str带有功能的名称和功能的idhex并打印出来.例如:

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

这篇关于&lt;在...上的功能是什么&gt;意思是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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