我们如何在Python中返回多个值?
可以从函数中以元组,列表,字典或用户定义类的对象的形式返回多个值
返回为元组
>>> def function():
a=10; b=10
return a,b
>>> x=function()
>>> type(x)
<class 'tuple'>
>>> x
(10, 10)
>>> x,y=function()
>>> x,y
(10, 10)返回列表
>>> def function():
a=10; b=10
return [a,b]
>>> x=function()
>>> x
[10, 10]
>>> type(x)
<class 'list'>返回字典
>>> def function():
d=dict()
a=10; b=10
d['a']=a; d['b']=b
return d
>>> x=function()
>>> x
{'a': 10, 'b': 10}
>>> type(x)
<class 'dict'>作为用户定义类的对象返回
>>> class tmp:
def __init__(self, a,b):
self.a=a
self.b=b
>>> def function():
a=10; b=10
t=tmp(a,b)
return t
>>> x=function()
>>> type(x)
<class '__main__.tmp'>
>>> x.a
10
>>> x.b
10热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短