In python, is there a "pass" equivalent for a variable assignment

Krystian Cybulski picture Krystian Cybulski · Sep 14, 2009 · Viewed 11k times · Source

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.

Answer

Unknown picture Unknown · Sep 14, 2009

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.