如何从Python中的函数返回多个值? [英] How to return more than one value from a function in Python?
本文介绍了如何从Python中的函数返回多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从Python函数中返回多个变量?
How to return more than one variable from a function in Python?
推荐答案
您将要返回的值分开逗号:
You separate the values you want to return by commas:
def get_name():
# you code
return first_name, last_name
逗号表示它是一个元组,因此您可以用圆括号包装您的值:
The commas indicate it's a tuple, so you could wrap your values by parentheses:
return (first_name, last_name)
然后,当您调用函数时,您a)将所有值保存为一个变量作为元组,或b)用逗号分隔变量名称
Then when you call the function you a) save all values to one variable as a tuple, or b) separate your variable names by commas
name = get_name() # this is a tuple
first_name, last_name = get_name()
(first_name, last_name) = get_name() # You can put parentheses, but I find it ugly
这篇关于如何从Python中的函数返回多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文