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)
<
Another possibility is to create a named tuple and use that in your return value. Then you can access the value you want by "name."
MyTuple = collections.namedtuple('MyTuple', 'ignored needed')
Then you can make
foo = (something_i_dont_need, something_i_need)
into
foo = MyTuple(something_i_dont_need, something_i_need)
and then say
x = foo.needed
instead of
x = foo[1]
Performance-wise, this may not be the best. But I think it can sometimes make writing and understanding the code easier. And it is an alternative to some of the other solutions.
1st case: foo needs two variable to unpack
(length of tuple is 2) . _ is perfectly ok.
2nd case: gives you index value
(slicing)
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. :)
I think the usual way of doing it
x=foo[index]
Using _
is less common, and I think also discouraged. Using _
is also unwieldy when you need only a few elements out of a long tuple
/list
. Slicing also comes handy when you are only choosing a contiguous subsequence.
But at the end of the day I think it is just a matter of subjective preference. Use whatever that looks more readable to you and your team.
Both are acceptable, and I've seen both in production code. I think the choice upon the context, the intent, and the local style.
There is also a third option where the code describes the unused value:
_real, imaginary = foo
I use all three within my code depending upon which is clearest:
_, x = foo
x=foo[i]
_real, imaginary = foo
foo
is a complex number.)If this is a return value from a function or method, another alternative is to write a wrapper function (or subclass the class and add a method) that returns just the item you're interested in, and calling that instead.