不能返回两个值,但可以返回元组或列表,然后进行解包:def select_choice():
...
return i, card # or [i, card]
my_i, my_card = select_choice()
如果希望返回两个以上的值,请考虑使用named tuple.它将允许调用函数的人按名称访问返回值的字段,这更容易读懂。仍然可以按索引访问元组的项。例如:data, errors = MySchema.loads(request.json())
if errors:
...
或result = MySchema.loads(request.json())
if result.errors:
...
else:
# use `result.data`
在其他情况下,您可以返回字典:def select_choice():
...
return {'i': i, 'card': card, 'other_field': other_field, ...}
但是,您可能需要考虑返回一个实用程序类的实例,该实例封装了您的数据:class ChoiceData():
def __init__(self, i, card, other_field, ...):
# you can put here some validation logic
self.i = i
self.card = card
self.other_field = other_field
...
def select_choice():
...
return ChoiceData(i, card, other_field, ...)
choice_data = select_choice()
print(choice_data.i, choice_data.card)