如何从Python中的函数返回两个值?

Lyc*_*con 189 python return function list return-value

我想从两个单独的变量中的函数返回两个值.例如:

def select_choice():
    loop = 1
    row = 0
    while loop == 1:
        print('''Choose from the following options?:
                 1. Row 1
                 2. Row 2
                 3. Row 3''')

        row = int(input("Which row would you like to move the card from?: "))
        if row == 1:
            i = 2
            card = list_a[-1]
        elif row == 2:
            i = 1
            card = list_b[-1]
        elif row == 3:
            i = 0
            card = list_c[-1]
        return i
        return card
Run Code Online (Sandbox Code Playgroud)

我希望能够分别使用这些值.当我尝试使用时return i, card,它返回一个tuple,这不是我想要的.

war*_*iuc 388

您不能返回两个值,但您可以在调用后返回a tuple或a list并将其解压缩:

def select_choice():
    ...
    return i, card  # or [i, card]

my_i, my_card = select_choice()
Run Code Online (Sandbox Code Playgroud)

在线return i, card i, card意味着创建一个元组.你也可以使用括号return (i, card),但是元组是用逗号创建的,所以parens不是必需的.但是您可以使用parens来使代码更具可读性,或者将元组拆分为多行.这同样适用于生产线my_i, my_card = select_choice().

如果要返回两个以上的值,请考虑使用命名元组.它将允许函数的调用者按名称访问返回值的字段,这更具可读性.您仍然可以通过索引访问元组的项目.例如,在Schema.loads方法Marshmallow框架中返回一个UnmarshalResult是a namedtuple.所以你可以这样做:

data, errors = MySchema.loads(request.json())
if errors:
    ...
Run Code Online (Sandbox Code Playgroud)

要么

result = MySchema.loads(request.json())
if result.errors:
    ...
else:
    # use `result.data`
Run Code Online (Sandbox Code Playgroud)

在其他情况下,您可以dict从您的函数返回:

def select_choice():
    ...
    return {'i': i, 'card': card, 'other_field': other_field, ...}
Run Code Online (Sandbox Code Playgroud)

但您可能需要考虑返回实用程序类的实例,该实例类包装您的数据:

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)
Run Code Online (Sandbox Code Playgroud)


Kar*_*tel 25

我想从两个单独的变量中的函数返回两个值.

你期望它在主叫端看起来像什么?你不能写,a = select_choice(); b = select_choice()因为这会调用该函数两次.

值不会在"变量"中返回; 这不是Python的工作方式.函数返回值(对象).变量只是给定上下文中值的名称.当您调用函数并在某处分配返回值时,您正在执行的操作是在调用上下文中为接收的值指定名称.该函数没有为您赋值"变量",赋值确实(更不用说变量不是值的"存储",而是一个名称).

当我试图使用return i, card它,它返回一个tuple,这不是我想要的.

实际上,这正是你想要的.你所要做的就是tuple再次分开.

我希望能够分别使用这些值.

所以只需抓住价值tuple.

最简单的方法是解压缩:

a, b = select_choice()
Run Code Online (Sandbox Code Playgroud)


hei*_*991 17

我觉得你想要的是一个元组.如果您使用return (i, card),您可以通过以下方式获得这两个结果:

i, card = select_choice()
Run Code Online (Sandbox Code Playgroud)


Ami*_*min 8

def test():
    ....
    return r1, r2, r3, ....

>> ret_val = test()
>> print ret_val
(r1, r2, r3, ....)
Run Code Online (Sandbox Code Playgroud)

现在你可以用你的元组做你喜欢的一切.


小智 5

def test():
    r1 = 1
    r2 = 2
    r3 = 3
    return r1, r2, r3

x,y,z = test()
print x
print y
print z


> test.py 
1
2
3
Run Code Online (Sandbox Code Playgroud)