Is there any recommended/generally accepted coding style for handling situations where a function returns a tuple of values but only one of those values is used afterwards (note that this is mostly meant for library functions I can't change -- writing a wrapper around the call is probably a bit of overkill…)? Instead of doing
a, b, c = foo()
and then just not using b
and c
, which of the following variants should be preferred (or is there another one?):
Variant 1 (underscore)
a, _, _ = foo()
(which is very clear and simple but might clash with _ = gettext.gettext
used in many applications that use translation)
Variant 2 (dummy name)
a, unused, unused = foo()
(not very appealing, I think, same goes for other names like dummy
)
Variant 3 (index)
a = foo()[0]
(to me the ()[0]
looks un-pythonic…)
a, b = foo()[0:2]
would work? If yes: yes, it does :)a, *_ = foo()
will throw away all the values except the first one.