Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
def DD():
aa = "ddcc"
dd = 2
cc = 3
return aa,dd,cc
result, _, _ = DD()
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()
Another option for variety:
>>> def DD():
... aa = "ddcc"
... dd = 2
... cc = 3
... return aa,dd,cc
...
>>> DD()[0]
'ddcc'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question