Ignore Python Multiple Return Value

Ignore python multiple return value

One common convention is to use a "_" as a variable name for the elements of the tuple you wish to ignore. For instance:

def f():
return 1, 2, 3

_, _, x = f()

How to elegantly ignore some return values of a Python function?

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.

How to ignore an output of a multi-output function in Python?

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]

Multiple return values, just one 'if' checking functions return

When you return multiple values, you're actually returning a tuple containing each of those values.

Your if test will return True regardless of the values (even if they are both None)

How to ignore the rest of the parameters that return by the function?

The recommended way of doing this is to use the _ variable name, as indicated by Abdul Niyas P M, this will not store the values captured by the _.

x, _, z = 1, 2, 3 # if x and z are necessary
# or
x, *_ = 1, 2, 3 # if only x is necessary

What is the most standard way of ignoring output from a python's function?

Just ignore the output:

foo()

Another option (which personally, I don't like too much) is assign the output to a variable '_':

_ = foo()

This is a conventional way to say that the returned value is ignored / unneeded, and is usually used in for loops:

lst = [1, 2, 3]
for _ in lst:
# do something unrelated to the current element in the list
print 'hi'

optional multiple return values

You can use star unpacking to gather all additional return values into a list:

x, *y = fun()

x will contain the first return value. y will be a list of the remaining values. y will be empty if there is only one return value. This particular example will only work if the function returns a tuple, even if there is only one value.

When fun always returns 1 or 2 values, you can just do

if y:
print(y[0])
else:
print('only one value')

If, on the other hand, you want to completely ignore the number of return values, do

*x = fun()

Now all the arguments will be gathered into the list. You can then print it with either

print(x)

or

print(*x)

The latter will pass each element as a separate argument, exactly as if you did

x, y, z = fun()
print(x, y, z)

The reason to use *x = fun() instead of just x = fun() is to get an error immediately when a function returns something that isn't a tuple. Think of it as an assertion to remind you to write fun properly.

Since this form of star unpacking only works in Python 3, your only option in Python 2 is to do

x = fun()

and to inspect the result manually.



Related Topics



Leave a reply



Submit