Is _ variable in lua special?

I am new to lua / torch. I notice that the variable _ is used a lot, especially in iterators. Example:

for _, node in ipairs(protos.rnn.forwardnodes) do a, b = whatever(a,b) end 

this "variable naming convention" (so to speak) is also used in other circumstances, for example:

 local _,loss = optimizer(feval,params, optim_state) 

Is there any particular meaning or is it just another variable name among the many possible names?

+5
source share
2 answers

It is commonly used as a throwaway variable. It does not have a β€œreal” special meaning, but is used to indicate that the indicated value does not matter.

A variable consisting only of the underscore "_" is usually used as a placeholder when you want to ignore a variable ...

More details here (under the naming part).

+8
source

Using _ is usually for return values ​​that you do not want to use from a function . What makes sense is like a space. The reason it is commonly used in iteration is because most iterators return key pairs, values, and you only need a value.

However, _ can also be used for the exact opposite. When placed behind a variable, such as _G or _VERSION , this means that it is important and should not be changed.

And finally, double underlining. I used them only for metamethods such as __index or __add , so if you are creating a function or API or something that checks your own metamethod, be sure to use consistent double underscores.

So, in the end, it's just a naming convention, and it's completely self-confident and optional.

+8
source

Source: https://habr.com/ru/post/1239275/


All Articles