P
P
PyTime_Sparrow2018-09-27 23:23:38
Python
PyTime_Sparrow, 2018-09-27 23:23:38

How to return a specific value from a function?

Let's say there is a function that returns 3 variables:

def DD():
    aa = "ddcc"
    dd = 2
    cc = 3
    return aa,dd,cc

is it possible to deduce this function so that it would return only the first variable?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
mark okolov, 2018-09-27
@okolovmark

def DD():
    aa = "ddcc"
    dd = 2
    cc = 3
    return aa,dd,cc
result, _, _ = DD()

like this

V
Vadim Shatalov, 2018-09-27
@netpastor

In addition to the fact that you can simply take only the first return value, you can also hang a decorator on the function that will do the same thing
. Something like

def get_first_value(fn):
    def wrapped():
        return fn()[0]
    return wrapped

@get_first_value
def DD():
    aa = "ddcc"
    dd = 2
    cc = 3
    return aa,dd,cc

first_value = DD()

V
Vladimir Kuts, 2018-09-27
@fox_12

Another option for variety:

>>> def DD():
...     aa = "ddcc"
...     dd = 2
...     cc = 3
...     return aa,dd,cc
...
>>> DD()[0]
'ddcc'

I
Igor Statkevich, 2018-09-30
@MadInc

can be unpacked
a, b, c = DD()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question