忽略python多重返回值 [英] Ignore python multiple return value

查看:145
本文介绍了忽略python多重返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个返回元组中多个值的Python函数:

  def func():
返回1,2

是否有一种很好的方式可以忽略其中一个结果,而不仅仅指定给临时变量?如果我只对第一个值感兴趣,有没有比这更好的方法:

  x,temp = func() 


解决方案

一个常见的惯例是使用_您希望忽略的元组元素的变量名称。例如:

  def f():
返回1,2,3

_,_,x = f()


Say I have a Python function that returns multiple values in a tuple:

def func():
    return 1, 2

Is there a nice way to ignore one of the results rather than just assigning to a temporary variable? Say if I was only interested in the first value, is there a better way than this:

x, temp = func()

解决方案

One common convention is to use a "_" as a variable name for the elements of the tuple you wish to ignore. For instance:

def f():
    return 1, 2, 3

_, _, x = f()

这篇关于忽略python多重返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆