I have a K function that returns three values (a,b and c) and I am using it in multiple places in my programme. I also want to use this function inside an H function. However, when I use it inside H function, I want it to return only its first two return values (a and b), just like in the code below. Is there a way with which I can hide the c when I use K inside H? Or should I just redefine K function inside H function separately in such a way that it returns only a and b values?
def K(x):
...
return a,b,c
def H(y):
...
a,b=K(y)
...
return p
Thanks!
You can use underscore '_'
def foo():
return 3,4,5
x,_,_ = foo()
print(x)
output
3
If the values you are returning are the first ones (eg only a, or only a and b), then your code will work as is.
If the values you are returning aren't the first ones (eg only b, or a and c), you'd need to use something like a, _, c = K(y)
(the _
is the common sign of a "dummy" variable).
You can also add a type check in the Function K:
def K(x, return_only_ab=False): # add default parameter return_only_ab
...
if not return_only_ab: # if False then return all the three variables
return a,b,c
else: # else reutrn only a, b that you need
return a, b
def H(y):
...
a,b=K(y, return_only_ab=True) # here you ll only get a,b and then do something with it
...
return p
hope it helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.