为什么解压这个map对象会显示“必须是iterable,而不是map”?

2024-04-26 22:54:31 发布

您现在位置:Python中文网/ 问答频道 /正文

这是怎么回事?在

>>> list(map(lambda *x: x, *map(None, 'abc')))
Traceback (most recent call last):
  File "<pyshell#52>", line 1, in <module>
    list(map(lambda *x: x, *map(None, 'abc')))
TypeError: type object argument after * must be an iterable, not map

忽略代码的无意义。这是关于错误消息,“iterable,not map”。地图是可编辑的,不是吗?在

如果我只将None替换为str,整个过程都很好:

^{pr2}$

所以现在Python对map没有任何问题。在

这在我的python3.6.1中发生过。相反,我的python3.5.2引发了预期的TypeError: 'NoneType' object is not callable。而google搜索"must be an iterable, not map"根本找不到任何结果。很明显这是最近才被介绍的。在

这只是一个Python bug吗?或者这有什么意义吗?在

更新:Reported as bug如建议。在


Tags: lambdanoneanmapobjectnotbeiterable
1条回答
网友
1楼 · 发布于 2024-04-26 22:54:31

我认为这是个虫子。以下是导致此异常的原因:

https://github.com/python/cpython/blob/b1660800f4f519dbfab9e5a4ad3eae1cfabab3ed/Python/ceval.c#L2514-L2523

对python字节码的反汇编确认它正在使用BUILD_TUPLE_UNPACK_WITH_CALL

上面代码中的“bug”是假设任何TypeError,而_PyList_Extend参数数组意味着它不是iterable,但是__iter__本身可能引发类型错误。它正在重新抛出这个异常

我建议在https://bugs.python.org打开一个bug

相关问题 更多 >