[Memo] Substitute unnecessary variables for "_" (underscore)

Introduction

I'm reading Python code and seeing the code that returns "_" (underscore)? I wondered and investigated it, so I will leave it as a memo.

Ignore Return value

After a lot of research, I found the following sentences.

Python has many libraries, and I often import and use functions. In such a case, if there are multiple return values ​​from the function and there is a part that is not used, you can use an underscore to discard the return value without occupying the memory.

In other words, it meant ignoring the return value.

As an example, it seems to be used as follows.

def get_tuple():
    return ('amedama', 'Japan/Tokyo')


def print_name():
    name, _ = get_tuple()
    print name

if __name__ == '__main__':
    print_name()

As long as the return value of the function is a tuple, there is no choice but to receive it as a tuple, but it seems that it can convey the intention that the value of the variable starting with the underscore does not need to be conscious (not used).

References

Recommended Posts

[Memo] Substitute unnecessary variables for "_" (underscore)
[For memo] Linux Part 2
heroku memo (for myself)
Python memo (for myself): Array
Python code memo for yourself
[For AtCoder] Standard input memo