【问题标题】:What is the specific name of this output?这个输出的具体名称是什么?
【发布时间】:2016-11-03 04:41:07
【问题描述】:
我制作了一个名为“function”的函数,如下所示。
>>> def function():
return 'hello world'
>>> function
<function function at 0x7fac99db3048> #this is the output
这个输出到底是什么?是具体的名字吗?它的意义是什么?
我知道它提供了有关内存位置的信息。但我需要有关此输出的更多信息。
高阶函数在返回函数时是否返回相似的数据?
【问题讨论】:
标签:
python
function
higher-order-functions
【解决方案1】:
在 python 中,函数是一个对象,因此当您调用 function 时,它会返回内存地址。高阶函数的行为方式相同。但是有一些区别:
def a():
print("Hello, World!")
def b():
return a
>>> a
<function a at 0x7f8bd15ce668>
>>> b
<function b at 0x7f8bd15ce6e0>
c = b
>>>c
<function b at 0x7f8bd15ce6e0>
c = b()
<function a at 0x7f8bd15ce668>
注意函数c 在不同情况下返回的内容。
【解决方案2】:
要调用该函数,您需要使用() 调用它。如果没有它,您将看到对function 函数的引用 存储在0x7fac99db3048。您也可以将其存储在另一个变量中:
>>> my_new = function # store function object in different variable
>>> function
<function function at 0x10502bc80>
# ^ memory address of my system
>>> my_new
<function function at 0x10502bc80>
# ^ same as above
>>> my_new() # performs same task
'hello world'
让我们看看另一个名称不是function的函数显示的内容:
>>> def hello_world():
... print 'hello world'
...
>>> hello_world
# v Name of function
<function hello_world at 0x105027758>
# ^ says object of type 'function'|^- memory address of function
# (for eg: for class says 'class')|