Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

core

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.

like image 368
Krystian Cybulski Avatar asked Sep 13 '09 23:09

Krystian Cybulski


People also ask

How do you pass variables in Python?

If a Python newcomer wanted to know about passing by ref/val, then the takeaway from this answer is: 1- You can use the reference that a function receives as its arguments, to modify the 'outside' value of a variable, as long as you don't reassign the parameter to refer to a new object.

Does Python have pass by reference?

All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function.

How does variable assignment work in Python?

Summary. To summarize: Python lets you create variables simply by assigning a value to the variable, without the need to declare the variable upfront. The value assigned to a variable determines the variable type. Different types may support some operations which others don't.

What is pass by value in Python?

Pass by value – It means that the value is directly passed as the value to the argument of the function. Here, the operation is done on the value and then the value is stored at the address. Pass by value is used for a copy of the variable.


5 Answers

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.

like image 191
Unknown Avatar answered Sep 21 '22 17:09

Unknown


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!

like image 31
Alex Martelli Avatar answered Sep 21 '22 17:09

Alex Martelli


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).

like image 23
Brent Writes Code Avatar answered Sep 25 '22 17:09

Brent Writes Code


We often do this.

count, _ = get_count_and_price()

or this

count, junk = get_count_and_price()
like image 33
S.Lott Avatar answered Sep 21 '22 17:09

S.Lott


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()
like image 31
Lambda Fairy Avatar answered Sep 24 '22 17:09

Lambda Fairy