11

Suppose I have a function:

def f():
  ...
  ...
  ...
  return a,b,c

and I want to get only b in output of the function. Which assignment I have to use?

Thanks.

Cupitor
  • 11,007
  • 19
  • 65
  • 91

3 Answers3

32

You can unpack the returned tuple into some dummy variables:

_, keep_this, _ = f()

It doesn't have to be _, just something obviously unused.

(Don't use _ as a dummy name in the interactive interpreter, there it is used for holding the last result.)

 

Alternatively, index the returned tuple:

keep_this = f()[1]
Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
  • 1
    Beat me to it by a couple seconds! Darn! – feralin Mar 24 '13 at 19:53
  • I cannot find any documentation for dummy variables. Any helps? – Cupitor Mar 24 '13 at 19:54
  • 1
    @Naji basically, what the code does is assign the three parts of the tuple to three variables. A single underscore is a valid variable name, and it can be assigned multiple times in one destructuring assignment. So when that assignment is complete, you will have two variables: keep_this, which equals the middle value in the tuple, and _, which (if I'm correct) equals the last value in the tuple. You should look up destructuring assignment to learn more. – feralin Mar 24 '13 at 19:56
  • 1
    There is nothing special about these variables. You could write `x, y, z = f()` and just ignore `x` and `z`. The name `_` is just unlikely to be useful for anything else. – Pavel Anossov Mar 24 '13 at 19:57
  • But then this is really not a good method. I might have a function which in certain situations returns a huge matrix but I rarely need it to be returned. Every time assigning a variable to it is not cool! – Cupitor Mar 24 '13 at 19:58
  • @PavelAnossov in a lot of functional programming languages, the name _, when it appears in a pattern, acts as a wildcard. In Python, _ is just another variable name, but in other languages, it has special meaning. – feralin Mar 24 '13 at 19:59
  • 2
    If your function returns a huge matrix that matrix already exists (unlike lazy evaluation such as Haskell uses); you can discard your reference to it with `del _` if you wish. BTW, `_` is a special variable in the interactive interpreter, holding the last returned value. – Yann Vernier Mar 24 '13 at 20:01
  • 1
    @Naji The function will return everything it has to return, you can't do anything about it. If it returns a matrix, and you need only a single value from it, use the second method. – Pavel Anossov Mar 24 '13 at 20:01
4
def f():
    return 1,2,3

here, f() returns a tuple (1,2,3) so you can do something like this:

a = f()[0]
print a
1

a = f()[1]
print a
2

a = f()[2]
print a
3
zenpoy
  • 19,490
  • 9
  • 60
  • 87
1

What you are doing with the return a,b,c line is creating a tuple and then returning it from the function. So you can use tuple deconstruction to get only the values you want from the result. Example:

def f(x):
    return x, x + 1, x + 2
_, b, _ = f(0)
print(b) # prints "1"
Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
feralin
  • 3,268
  • 3
  • 21
  • 37