python - Python 中的返回值

标签 python return return-value

关于 bretcret 的值,我希望我的函数返回 1、2 或 3 个值。 如何做比这更聪明的事情? :

def myfunc (a, bret=False, cret=False):
  b=0
  c=0
  if bret and cret:
   return a,b,c
  elif bret and not cret:
   return a,b
  elif not bret and cret:
   return a,c
  else:
   return a

最佳答案

怎么样:

def myfunc(a, bret=False, cret=False):
   ...
   return (a,) + ((b,) if bret else ()) + ((c,) if cret else ())

关于python - Python 中的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20442042/

相关文章:

Python struct 使用反向字节顺序解压多个字节

javascript - Google 跟踪代码管理器自定义变量返回未定义

java - 在类中返回所需的值 - Java

shell - 从 shell 脚本中调用的函数返回值

python - 无法将列表映射到 python 中的列表列表

python - 使用 linalg 查找特征向量

javascript - 在JavaScript中将多个事件监听器添加到元素

python - 在函数中返回变量 Python 无法正常工作

python - 如何从 NTP 服务器获取时间?

c - 警告 : control reaches end of non-void function [-Wreturn-type]