Extracting the a value from a tuple when the other values are unused

前端 未结 6 1288
一整个雨季
一整个雨季 2021-02-04 10:42

I have a tuple foo which contains something I don\'t care about and something I do.

foo = (something_i_dont_need, something_i_need)
<
6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 10:58

    I've been using _ for over a decade. It is much more readable, especially when extracting more than one value:

      _, _, name, _, _, city, _ = whatever
    

    Even with only one variable, the other way forces humans readers to count if they want to truly understand the code, and more likely their eyes are just going to pass over it.

    With the underscores, you can leverage the human brain's pattern matching abilities a little better. Probably a small thing, but every little bit helps when you're debugging. :)

提交回复
热议问题