0
import random

def some_function():
    example = random.randint(0, 1)
    if example == 1:
        other_example = 2
    else:
        return False
    return example, other_example

在此示例中,可能会返回一个或两个变量。通常,对于一个变量,我会使用 var = some_function()while 来表示两个,var, var2 = some_function(). 如何判断函数返回了多少变量?

4

5 回答 5

2

try something like this:

>>> lis=(1,)[:2] #just one value, 
>>> lis
(1,)


>>> lis=(1,2)[:2]   #two values
>>> lis
(1, 2)


>>> lis=(1,2,3,4,5)[:2]  #any number of values
>>> lis
(1, 2)

# used [:2] here, because you only expect a maximum of 2 values
# use [:] to collect any number of values                  

But the only problem with this approach is that you need to return a tuple at every return statement, so return False becomes return False,

using iter(), this too expect that you always return a tuple or any iterable:

>>> ret=((1,))
>>> ret=iter((1,))
>>> a,b=ret.next(),list(ret)[:1]  #used [:1] coz we need to store only 2 elements
>>> a,b
(1, [])


>>> ret=iter((1,2))
>>> a,b=ret.next(),list(ret)[:1]
>>> a,b
(1, [2])

>>> ret=iter((1,2,3,4))
>>> a,b=ret.next(),list(ret)[:1]
>>> a,b
(1, [2])
于 2012-09-30T21:28:01.310 回答
2

There's no guaranteed way to tell the difference between a function that returns a single tuple and one that returns multiple non-tuple values. If you know that the function doesn't return a tuple, though, then you can use this code to check for multiple return values:

return_val = some_function()
if isinstance(return_val, tuple):
    # number of returned values is len(return_val)
    if len(return_val) == 2:
        var, var2 = return_val
else:
    # returned a single non-tuple item
    var = return_val

The best way to handle this, however, is to write functions that don't return different numbers of values in different cases. Instead, you can do something like...

def some_function():
    example = random.randint(0, 1)
    if example == 1:
        other_example = 2
    else:
        return False, None
    return example, other_example

...so that the function always returns two values. Alternatively, you can always return a single tuple, and just check for the number of elements in that tuple (so for instance, returning an empty tuple () or a tuple with a single false value (False,).

于 2012-09-30T21:28:38.013 回答
1
var_returned = some_function()
num_vars = len(vars_returned) if (not vars_returned == False) else 0

ternary then! this can be done even shorter.

vars_returned = 7, 2
num_vars = len(vars_returned) if vars_returned else 0
print(num_vars)
# 2

vars_returned = False
num_vars = len(vars_returned) if vars_returned else 0
print(num_vars)
# 0 
于 2012-09-30T21:29:44.443 回答
1

为什么不试试这个:

import random

def some_function():
    example = random.randint(0, 1)
    if example == 1:
        other_example = 2
    else:
        return ()
    return example, other_example

这样,您将始终将元组作为返回值,但是当您需要时,元组将测试为 False。像这样使用它:

answer = some_function()
if answer:
     var1, var2 = answer
于 2012-09-30T21:35:26.473 回答
1

让函数返回明显不同的类型是不好的做法。通常,如果一个函数返回元组,它将始终返回元组,并且它们将始终具有相同数量的项目,除非它们返回None。原因正是促使您提出这个问题的原因:很难解释给定的返回值。在任何地方调用你的函数,你都会有解码返回值的开销。并且任何文档都必须解释每种情况下的预期输出。

有很多合理的选择。您可以返回将数据包装在某些上下文中的对象、字典或collections.namedtuple。或者,对于大多数真实世界的代码,您可以将其划分为更易于理解的不同行的函数。

于 2012-09-30T23:03:08.497 回答