简体   繁体   English

在Python的另一个函数中使用函数时,是否可以隐藏函数的某些返回值?

[英]Can I hide some of my function's return values when I use it inside another function in Python?

I have a K function that returns three values (a,b and c) and I am using it in multiple places in my programme. 我有一个返回三个值(a,b和c)的K函数,并且在程序中的多个地方都在使用它。 I also want to use this function inside an H function. 我也想在H函数中使用此函数。 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. 但是,当我在H函数中使用它时,我希望它仅返回其前两个返回值(a和b),就像下面的代码一样。 Is there a way with which I can hide the c when I use K inside H? 当我在H中使用K时,是否可以隐藏c? Or should I just redefine K function inside H function separately in such a way that it returns only a and b values? 还是应该只在H函数中重新定义K函数,使其仅返回a和b值?

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 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. 如果您返回的值是第一个值(例如,仅a,或仅a和b),那么您的代码将按原样工作。

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). 如果您返回的值不是第一个(例如,只有b或a和c),则需要使用a, _, c = K(y)_是a的公称“虚拟变量)。

You can also add a type check in the Function K: 您还可以在功能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 希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用另一个 class 内部的 function 中的变量吗? - Can I use a variable from a function that's inside of another class? 当 python 没有返回时,如何访问另一个 function 中的值? - How can I access value in another function when there is no return at python? 如何在 Python 3 中获取一个函数以返回我可以在另一个函数中使用的值? - How can I get a function in Python 3 to return a value that I can use in another function? 似乎我不能在另一个函数中使用一个函数 - It seems i can't use a function inside another function 我如何在另一个函数中使用一个函数 - How can i use a function inside of another function Python:当我在另一个函数中调用一个函数时,没有变化 - Python: No change when I call a function inside another function 在Python中,如何在第二个函数中使用函数的return语句而不使用全局变量? - In Python, how can I use a function's return statement within a second function without using global variables? 我在 python 中有一个方法/函数,我试图在其中获取一个返回值,然后我可以将其用作另一个函数中的变量 - I have a method / function in python in which i'm trying to get a return value which i can then use as a variable in another function 无法使创建的 python function 返回一个值,然后我可以在另一个计算中使用该值 - Can't make created python function return a value which I can then use in another calculation 如何正确转换列表的输出值以将它们用作函数内的参数? - How can I properly convert a list's output values to use them as args inside a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM