4

Suppose I have a very simple function:

def myFunction(arg1, arg2):
    return arg1, arg2

Now if I only want to access the second argument, can do I something like:

none, arg2 = myFunction(arg1, arg2)

?

Michał Czapliński
  • 1,332
  • 1
  • 14
  • 24

2 Answers2

12

Yes, yes you can do something like that. Exactly that, actually.

Assign the argument you want to ignore to a name you then simply not use anywhere else. The _ name is often used:

_, arg2 = myFunction(arg1, arg2)

The _ name is just a convention, but most Python developers understand it to mean 'this variable will be ignored by the rest of the code, it is just there to fulfill a tuple assignment'.

Alternatively, index or slice the output:

arg2 = myFunction(arg1, arg2)[-1]

or for a function with more than two return values:

arg2, arg3 = returnsMoreThanTwo(arg1, arg2)[-2:]

but if you wanted to ignore an argument in the middle somewhere, the _ assignment is best. You can use it multiple times without penalty:

arg1, _, _, arg4 = returnsFourArguments(arg1, arg2)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Or where missing out a lot of stuff in the middle...`first_and_something = operator.itemgetter(0, 60)` then `first, something = first_and_something(returnsWhateverArguments(arg1, arg2))` or whatever... – Jon Clements Nov 29 '13 at 14:13
  • @JonClements: In such cases, just assigning the output to a local, then picking out specific indices would be a little more readable. Unless you need to do this for 3 or more different function calls. Then the `itemgetter()` approach is fair game. :-P – Martijn Pieters Nov 29 '13 at 14:18
4
_, arg2 = myFunction(arg1, arg2)
halfer
  • 19,824
  • 17
  • 99
  • 186
Ellioh
  • 5,162
  • 2
  • 20
  • 33
  • 4
    Who downvoted this? It's a valid, correct answer. It's the same as Martijn's but was posted only a few seconds after. – Joe Nov 29 '13 at 13:54
  • Even more. At the moment I have posted his answer, Martijn's text was different from what it's now. – Ellioh Nov 29 '13 at 13:57
  • 2
    A downvote and a delete vote... wow – Jon Clements Nov 29 '13 at 13:57
  • @JonClements 2 downvotes, actually. – devnull Nov 29 '13 at 13:58
  • 2
    @Ellioh: but it did include an explanation of *why* `_` can be used. Not that I voted on your post.. – Martijn Pieters Nov 29 '13 at 13:58
  • 4
    @MartijnPieters That's maybe a reason not to upvote, but not a valid reason to downvote imho. – Joe Nov 29 '13 at 13:59
  • @Martijn Pieters: agree. You explained the details, and I have nothing against it. I just can't understand those who downvoted. – Ellioh Nov 29 '13 at 13:59
  • @Joe: I entirely agree with that; I just wanted to clarify that my answer wasn't quite *exactly* the same. :-) – Martijn Pieters Nov 29 '13 at 14:00
  • 3
    +1 from me. Actually, I've seen this rate of meaningless downvotes only in `python` questions. Never seen something like this in `postgresql` or `sql-server`. Don't know why. OTOH, it works for upvotes too - once I had answer about how to join list into string (like `' '.join(a)`) with 11 upvotes!!! To get 11 upvotes in `sql-server` section I have to create something really cool :) – Roman Pekar Nov 29 '13 at 14:10
  • @RomanPekar Yep, answers with most research effort and knowledge put in rarely exceeds +3/+4. And simple onliners can bump to +11 with ease :) – alko Nov 29 '13 at 14:12
  • @RomanPekar: sad, but true. When your answer contains some kind of a research, it has little chance of getting +11. And this completely trivial answer has already brought me +5-2 = 46 reputation. :-D – Ellioh Nov 29 '13 at 14:14
  • You want really ridiculous upvoting? This question is actually a duplicate (I suggest people vote to close this question). The accepted answer there has +40 upvotes. – Joe Nov 29 '13 at 14:18