30

I am using a library function called get_count_and_price which returns a 2-tuple (count,price). In many places I use both time and price. However, in some I only need time or price. So right now, if I only need count, I assign to (count,price) and leave the price unused.

This works great and causes no trouble in and of itself.

However...

I use Eclipse with PyDev, and the new version 1.5 automatically shows errors and warnings. One of the warnings it shows is unused variables. In the above example, it flags price as unused. This is the sort of behavior which is great and I really appreciate PyDev doing this for me. However, I would like to skip the assignment to price altogether. Ideally, I would like something like:

(count,None) = get_count_and_price()

Now as we all know, None cannot be assigned to. Is there something else I could do in this case?

I know I could do something like

count = get_count_and_price()[0]

but I am asking just to see if anyone has any better suggestions.

Krystian Cybulski
  • 10,789
  • 12
  • 67
  • 98

6 Answers6

33

I think there's nothing wrong with using the [0] subscript, but sometimes people use the "throwaway" variable _. It's actually just like any other variable (with special usage in the console), except that some Python users decided to have it be "throwaway" as a convention.

count, _  = get_count_and_price()

About the PyDev problem, you should just use the [0] subscript anyway. But if you really want to use _ the only solution is to disable the unused variable warnings if that bothers you.

Unknown
  • 45,913
  • 27
  • 138
  • 182
  • 8
    be cautious about this. Remember that in an interactive python interpreter, `_` has a special meaning, and assigning to it takes that magic away. – SingleNegationElimination Sep 13 '09 at 23:10
  • @TokenMacGuy: yes I know, I wrote about that in a similar question here http://stackoverflow.com/questions/818828/is-it-possible-to-implement-a-python-for-range-loop-without-an-iterator-variable/818836#818836 – Unknown Sep 13 '09 at 23:12
  • 6
    Haskell and Erlang also use _ as the "throwaway" variable. – Mark Rushakoff Sep 13 '09 at 23:18
  • 8
    As long as you don't have gettext as `_()` – u0b34a0f6ae Sep 14 '09 at 00:00
  • 2
    Also found this handy comment/annotation which will make PyDev ignore such warning for the lines which have unused variables: #@UnusedVariable – Krystian Cybulski Sep 14 '09 at 01:02
  • 3
    Because of the above mentioned issues with \_, I usually use a double underscore(\_\_): count, \_\_ = get_count_and_price() – jcdyer Sep 14 '09 at 15:18
21

Using _ as severally proposed may have some issues (though it's mostly OK). By the Python style guidelines we use at work I'd normally use count, unused_price = ... since pylint is configured to ignore assignments to barenames starting with unused_ (and warn on USE of any such barename instead!-). But I don't know how to instruct PyDev to behave that way!

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 4
    I just upgraded to PyDev 2.2.2 and it seems that it now recognizes the `unused_` convention by default and doesn't give any warning – Jonathan Livni Sep 01 '11 at 07:06
7

If you go to the Eclipse -> Preferences… window, you can actually specify which variable names PyDev should ignore if they're unused (I'm looking at the newest PyDev 1.5.X).

If you go to PyDev -> Editor -> Code Analysis and look at the last field that says "Don't report unused variable if name starts with"

Enter whatever names you want in there and then use that name to restrict what variable names PyDev will ignore unused warnings for.

By default, it looks like PyDev will hide unused variable warnings for any variables that have names beginning with "dummy", "_", or "unused".

As @TokenMacGuy said below, I'd recommend against using just "_" because it has special meaning in certain scenarios in Python (specifically it's used in the interactive interpreter).

Brent Writes Code
  • 19,075
  • 7
  • 52
  • 56
  • While you do make a good point about `_` having special meaning in the interpreter, my impression is that it's fairly widely understood to have a different special meaning (if only by convention) in written code. – David Z Sep 14 '09 at 00:17
3

We often do this.

count, _ = get_count_and_price()

or this

count, junk = get_count_and_price()
S.Lott
  • 384,516
  • 81
  • 508
  • 779
3

I'd rather name it _price instead, for these reasons:

  • It solves the conflict with gettext and the interactive prompt, which both use _

  • It's easy to change back into price if you end up needing it later.

  • As others have pointed out, the leading underscore already has a connotation of "internal" or "unused" in many languages.

So your code would end up looking like this:

(count, _price) = get_count_and_price()
Lambda Fairy
  • 13,814
  • 7
  • 42
  • 68
1

I'll go after a Necromancer badge. :)

You said you're using PyDev. In PyDev (at least recent versions - I didn't check how far back), any variable name that starts with "unused" will be exempt from the Unused Variable warning. Other static analysis tool may still complain, though (pyflakes does - but it seems to ignore this warning in a tuple-unpacking context anyway).

mikenerone
  • 1,937
  • 3
  • 15
  • 19