首页 > 解决方案 > 我得到: TypeError: cannot unpack non-iterable NoneType object,但我正在返回一个元组

问题描述

我收到以下错误:

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,并且该值无法迭代。但在这种情况下,我期待并返回一个元组。

标签: python

解决方案


首先,您在执行该行时不会收到您所说的错误:

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_schis 也是如此None。另一方面,如果你的insert_t1函数 WAS 返回None,你会得到你所看到的错误。只需在上面的代码中替换return client_sch, Truereturn None,您就会得到您报告的错误。

所以我猜你在这里展示的内容与你的实际情况不符。如果您还没有,我建议您寻找其他insert_t1可以返回的方法,并可能在调试器中运行您的代码。如果你得到你所说的错误,那么我提供的代码也应该给你那个错误。如果是这样,那么就会发生一些奇怪的事情。如果没有,那么显然有一些事情在您提供的内容中不可见。


推荐阅读