3 回答

TA贡献1830条经验 获得超3个赞
的()
意思是“现在调用这个函数”。
print(str.upper())
引用没有 的函数不会()
立即调用该函数。
map(str.upper)
该str.upper
功能是传递给map
函数。该map
功能现在可以决定什么用呢str.upper
功能。它可能会调用一次或多次,或者将其存储在某处以备后用。

TA贡献1829条经验 获得超9个赞
使用时.apply(function_name),内部将 function_name 视为对已应用函数的引用。因此,只传递函数名,而且没有括号。
当它应用于实体时,它将从function_name.
就像将一个函数作为参数传递给另一个函数一样。
例子:
def fun(x):
return x*x
def fun_b(function):
return function(3)
print(fun_b(fun))

TA贡献1884条经验 获得超4个赞
在 Python 中,一切都是对象。不带括号的函数不会被计算并被视为一个对象。带括号的函数会立即求值。
例如:
>>> def return_one():
... return 1
...
>>> return_one # return the object reference, do not evaluate
<function return_one at 0x7fb76d3f4bf8>
>>> return_one() # evaluate the function
1
考虑到这一点,该.apply方法接受一个函数对象,然后在稍后的时间点调用它。如果您要编写.apply(str.upper()),str.upper()将评估为 astr而不是func对象。
添加回答
举报