Code question, what are "_"s for?

In C1_W2_Lab03_Feature_Scaling_and_Learning_Rate_Soln, I wondered what the “,,” on the left of an equality is for? The line of code is: _,_,hist = run_gradient_descent(X_train, y_train, 10, alpha = 9e-7)

week2

In Python, calling a function (in this case you suggest run_gradient_descent()) might be expected to return multiple terms (in this case it seems there are three return values).

The ‘_’ just says, ‘okay’, I don’t care about this return value, I know it is coming back, but just ignore it.

This is easier than other languages where you might have to create a ‘fake’ variable to store the return value that you will never use, or otherwise without that the return on your function will cause ‘errors/problems’.

4 Likes

I mostly concur with @Nevermnd ’s explanation. However, @rollther note that it is an idiom or programming style that works because the underscore is an allowed character for object names, also called identifiers in Python. a_a is allowed. So is _a_. But just the underscore alone, _ , is also an allowed name. Here, it is part of the assignment that is created by the function return, but you give it the name _ so it is clear you don’t intend to use its value in this scope. Note, however, that you could do. There is nothing special about the underscore as a name other than it isn’t very reader friendly. HTH

From the Python language doc

valid characters for identifiers are … the uppercase and lowercase letters A through Z , the underscore _ and, except for the first character, the digits 0 through 9 .

also

…Elsewhere, _ is a regular identifier. It is often used to name “special” items, but it is not special to Python itself.

3 Likes

That helps, all clear, thank you very much @Nevermnd & @ai_curious .

1 Like