我得到:TypeError:无法解包不可迭代的NoneType对象,但是我返回一个元组

时间:2019-04-20 14:42:42

标签: python

我遇到以下错误:

TypeError: cannot unpack non-iterable NoneType object

当我调用此函数时:

        # current client's schedule (dict)
        clientSchedule = client

        # value to let the program know if scheduling is 'finished'
        t1_processDone = False

        # the function itself, variable types are as follows:
        # client_name is a string, client_ID is an int,
        # client_team is a list, clientSchedule is a dict,
        # t1_processStatus is dynamically typed (True, False or None)
        clientSchedule, t1_processDone = insert_t1(client_name, client_ID,
                              client_team, clientSchedule, 
                              t1_processStatus)

此行发生错误:

clientSchedule, t1_processDone = insert_t1(client_name, client_ID...)

client_sch是一个不为空的字典。从我的研究中,我发现当函数返回None时会发生此错误,并且该值无法迭代。但是在这种情况下,我期望并返回一个元组。

1 个答案:

答案 0 :(得分:0)

首先,执行该行时不会出现您说的错误:

return client_sch, True

因为该行未进行任何拆包。如果您是指insert_t1通过该行返回时,在代码的最后一行出现错误,则更有意义。但是,如果这是真的,那么您不应该得到所得到的错误。这段代码运行良好:

def insert_t1(a, b, c, d, e):
    client_sch = None
    return client_sch, True

def main():

    client = ""
    client_name = ""
    client_ID = ""
    client_team = ""
    t1_processStatus = ""

    # current client's schedule (dict)
    clientSchedule = client

    # value to let the program know if scheduling is 'finished'
    t1_processDone = False

    # the function itself, variable types are as follows:
    # client_name is a string, client_ID is an int,
    # client_team is a list, clientSchedule is a dict,
    # t1_processStatus is dynamically typed (True, False or None)
    clientSchedule, t1_processDone = insert_t1(client_name, client_ID,
                                               client_team, clientSchedule,
                                               t1_processStatus)

main()

请注意,即使client_schNone,此方法也有效。另一方面,如果您的insert_t1函数返回了None,则会收到您看到的错误。只需在上面的代码中将return client_sch, True替换为return None,就可以准确地报告错误。

因此,我猜测您在此处呈现的内容与您的实际情况不符。如果还没有,我建议您寻找insert_t1可以返回的其他方式,并可能在调试器中运行您的代码。如果您遇到错误,则会得到您所说的话,那么我提供的代码也应该为您提供该错误。如果是这样,则说明发生了一些奇怪的事情。如果没有,那么显然您提供的内容中看不到正在发生的事情。