-3

I am trying to understand a code written in python. They are passing mean_squared_error to make_scorer. What does it mean to pass a function as an argument of another function? What does the underscore at the end of mean_squared_error_ represent?

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)
MAS
  • 4,503
  • 7
  • 32
  • 55
  • Well, yes, you can pass functions around just like regular values. The receiver is then free to call that function. *mind blown* – The `_` just serves to make the name different; apparently the author couldn't come up with a better name. – deceze Feb 17 '16 at 10:28
  • Not sure why the massive downvote hail. – Ami Tavory Feb 17 '16 at 10:30
  • @AmiTavory my guess would be that this question could easily be answered by googling his own title reading eg http://stackoverflow.com/questions/1349332/python-passing-a-function-into-another-function – M.T Feb 17 '16 at 10:33

1 Answers1

2
  1. In Python, everything is an object, including functions. A function foo is something that supports using it as foo(...). As opposed to some other languages, therefore, there is nothing special in Python about passing a function to a function. You're just passing a regular object, with the property that the other function can call it as a function.

  2. Adding an underscore at the end of an identifier, is a common Python practice for avoiding overwriting existing names. Note that the name of the first function here is fmean_squared_error. In the body, the writer used the same name but with an underscore appended, to avoid overwriting the function's identifier. Alternatively, this could have been named, say, res.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185