类型错误:“函数”类型的参数不可迭代

TypeError: ‘function’ 类型的参数不可迭代

TypeError: argument of type ‘function’ is not iterable

Python “TypeError: argument of type ‘function’ is not iterable”发生在我们对函数使用inornot in运算符但忘记调用它时。要解决错误,请确保调用该函数,例如my_func()

类型函数的类型错误参数不可迭代

下面是错误如何发生的示例。

主程序
def get_list(): return ['a', 'b', 'c'] # ⛔️ TypeError: argument of type 'function' is not iterable print('a' in get_list) # 👈️ forgot to call function
我们忘记用括号调用函数,例如get_list(),所以我们的代码试图检查函数中的成员而不是列表中的成员。

要解决错误,请确保调用该函数。

主程序
def get_list(): return ['a', 'b', 'c'] print('a' in get_list()) # 👉️ True print('a' not in get_list()) # 👉️ False

We used parentheses to invoke the function, so now we check for membership in
the list rather than the function.

The
in operator
tests for membership. For example, x in s evaluates to True if x is a
member of s, otherwise it evaluates to False.

main.py
my_str = 'hello world' print('world' in my_str) # 👉️ True print('another' in my_str) # 👉️ False

x not in s returns the negation of x in s.

All built-in sequences and set types support the in and not in operators.

When used with a dictionary, the operators check for the existence of the
specified key in the dict object.

Conclusion #

Python “TypeError: argument of type ‘function’ is not iterable”发生在我们对函数使用inornot in运算符但忘记调用它时。要解决错误,请确保调用该函数,例如my_func()