30

Sometimes a function gives a return value which you'd just want to discard rather than send to the output stream. What would be the elegant way to handle this?

Note that we're talking about a function that returns something which you cannot change.

def fn():
    return 5

I personally have used null before, but I'm looking for a more pythonic way:

null = fn()
PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
  • What do you mean by "send to the output stream"? – BrenBarn Oct 08 '14 at 07:58
  • Just evaluating `fn()` in the global scope would "print" the value 5. – PascalVKooten Oct 08 '14 at 07:58
  • 6
    That is only true in the interactive interpreter. It won't occur if you run a file as a script, and it doesn't really impact anything. I don't think the need to suppress output in an interactive session is a big enough deal to warrant anything more than the solution you already have. – BrenBarn Oct 08 '14 at 07:59
  • 3
    If you must, use `_ = fn()`. – Dan D. Oct 08 '14 at 08:01
  • @BrenBarn Ah, only in the interactive interpreter then. Well, in testing I'm printing now loads of crap. It would be good to know in general a way to avoid it. Furthermore, even in functions that doesn't print it (storing in a variable, or not using the interpreter) could be good to have a pythonic way to show that the result of a function is not important. – PascalVKooten Oct 08 '14 at 08:03
  • @DanD. I guess that might be an/the answer? – PascalVKooten Oct 08 '14 at 08:04
  • Does this answer your question? [Ignore python multiple return value](https://stackoverflow.com/questions/431866/ignore-python-multiple-return-value) – Josh Correia Feb 03 '21 at 19:42

2 Answers2

40

The standard way to show this is to assign the results you don't want to _. For instance, if a function returns two values but you only want the first, do this:

value, _ = myfunction(param)

Most Python linters will recognize the use of _ and not complain about unused variables.

If you want to ignore all returns, then simply don't assign to anything; Python doesn't do anything with the result unless you tell it to. Printing the result is a feature in the Python shell only.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 1
    You are not ignoring it, you are assigning it to a global variable. To quote from nosklo: "this "convention" sucks when you add gettext functionality to someone else's code (that defines a function called '_') so it should be banned". What if somebody uses their last output as an input? – JohnJohn Jan 24 '18 at 00:49
  • 1
    "you are assigning it to a global variable" -> No you aren't. The '_' in the python repl is a magic variable, interpreted by the REPL only. It doesn't exist within any script execution. (Try `del _` in the REPL, you get a NameError because it is not a variable). – Michael Leonard Oct 21 '18 at 04:55
9

I really like the idea of assigning to null - but it does have an unexpected side-effect:

>>> print divmod(5, 3)
(1, 2)
>>> x, null = divmod(5, 3)
>>> print null
2

For those who code mostly in Python, this is probably not an issue: null should be just as much a junk value as _.

However, if you're switching between other languages like Javascript, it's entirely possible to accidentally write a comparison with null instead of None. Rather than spitting the usual error about comparing with an unknown variable, Python will just silently accept it, and now you're getting true and false conditions that you totally don't expect... this is the sort of error where you stare at the code for an hour and can't figure out wth is wrong, until it suddenly hits you and you feel like an idiot.

The obvious fix doesn't work, either:

>>> x, None = divmod(5, 3)
File "<stdin>", line 1
SyntaxError: cannot assign to None

...which is really disappointing. Python (and every other language on the planet) allows all of the parameters to be implicitly thrown away, regardless of how many variables are returned:

>>> divmod(5, 3)

...but explicitly throwing away parameters by assigning them to None isn't allowed?

It feels kind of stupid of the Python interpreter to characterize assigning to None as a syntax error, rather than an intentional choice with an unambiguous result. Can anyone think of a rationale for that?

David Stein
  • 866
  • 7
  • 21