返回元组
def return_tuple():
return 'https://www.python.org/', 'https://docs.djangoproject.com/en/dev/', 'https://www.cnblogs.com/qingchengzi'
使用序列解包
def return_list():
return ['https://www.python.org/', 'https://docs.djangoproject.com/en/dev/', 'https://www.cnblogs.com/qingchengzi']
s, avg = return_list()
print(s)
print(avg)
使用类
class ReturnObject:
def __init__(self, value1, value2):
self.value1 = value1
self.value2 = value2
def return_object():
return ReturnObject('https://www.python.org/', 'https://docs.djangoproject.com/en/dev/')
使用字典
def return_dict():
return {'key1': 'https://www.python.org/', 'key2': 'https://docs.djangoproject.com/en/dev/'}
调用这些函数时,你可以使用以下方法接收返回值:
元组:直接赋值给多个变量。
序列解包:使用多个变量接收返回值。
类:创建类的实例并赋值给多个变量。
字典:通过键访问返回值。
例如,调用`return_tuple`函数并接收返回值:
url1, url2, url3 = return_tuple()
print(url1)
print(url2)
print(url3)
或者使用序列解包:
s, avg = return_list()
print(s)
print(avg)
希望这能帮助你理解如何在Python中返回和调用多个值