0

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!

3 Answers 3

4

You can use underscore '_'

def foo():
  return 3,4,5

x,_,_ = foo()
print(x)

output

3

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Glad I could help you. can you please accept the Answer so that it helps others when they make a search
0

If the values you are returning are the first ones (e.g. 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 (e.g. 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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.