将函数传递给python中的函数

时间:2016-02-17 10:22:17

标签: python

我试图理解用python编写的代码。他们将mean_squared_error传递给make_scorer。将函数作为另一个函数的参数传递是什么意思? mean_squared_error_末尾的下划线代表什么?

def fmean_squared_error(ground_truth, predictions):
    fmean_squared_error_ = mean_squared_error(ground_truth, predictions)**0.5
    return fmean_squared_error_

RMSE  = make_scorer(fmean_squared_error, greater_is_better=False)

1 个答案:

答案 0 :(得分:2)

  1. 在Python中,everything is an object,包括函数。函数foo支持将其用作foo(...)。因此,与其他一些语言相反,Python中将函数传递给函数没有什么特别之处。您只是传递一个常规对象,并使用另一个函数可以将其称为函数的属性。

  2. 在标识符末尾添加下划线是一种常见的Python实践,可避免覆盖现有名称。请注意,此处第一个函数的名称为fmean_squared_error。在正文中,编写者使用相同的名称,但附加了下划线,以避免覆盖函数的标识符。或者,可以将其命名为res