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

前端 未结 6 1285
一整个雨季
一整个雨季 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:49

    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.

    0 讨论(0)
  • 2021-02-04 10:49

    1st case: foo needs two variable to unpack (length of tuple is 2) . _ is perfectly ok.

    2nd case: gives you index value (slicing)

    0 讨论(0)
  • 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. :)

    0 讨论(0)
  • 2021-02-04 11:06

    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.

    0 讨论(0)
  • 2021-02-04 11:13

    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

    • When the tuple is small.
    • When there are few discarded values.
    • When there are few discarded values relative to the number of extracted values.
    • The reader probably knows the tuple's composition, and the composition of the whole tuple is important.
    • When it's customary to think about the structure of the tuple as a single unit

    x=foo[i]

    • When the tuple is large.
    • When there are many discarded values for some value of many.
    • When the reader probably knows the tuple's composition.
    • The rest of the tuple's values are completely and utterly irrelevant & offer no useful information for the reader.
    • When it's customary to think about the structure as a sequence.
    • When the tuple has uniform composition.
    • When it's customary to use an index for the datatype.
    • When the index is keyed in a loop.
    • When the reader's attention should be drawn to the index's value.

    _real, imaginary = foo

    • When the tuple is small.
    • When there are few discarded values.
    • When the reader probably doesn't know the tuple's composition.
    • When naming the discarded value gives the reader insight. (You can guess from this one line that foo is a complex number.)
    0 讨论(0)
  • 2021-02-04 11:14

    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.

    0 讨论(0)
提交回复
热议问题