简体   繁体   English

为什么解压缩这个地图对象打印“必须是可迭代的,而不是地图”?

[英]Why does unpacking this map object print “must be an iterable, not map”?

What is going on here? 这里发生了什么?

>>> 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

Ignore the senselessness of the code. 忽略代码的无意义。 This is about the error message, "iterable, not map" . 这是关于错误消息, “可迭代,而不是映射” Maps are iterables, are they not? 地图可迭代的,不是吗?

And if I only replace None with str , the whole thing works fine: 如果我只用str替换None ,整个过程都可以正常工作:

>>> list(map(lambda *x: x, *map(str, 'abc')))
[('a', 'b', 'c')]

So now Python doesn't have any issue with a map there after all. 所以,现在Python没有与任何问题map存在毕竟。

This happens in my Python 3.6.1. 这发生在我的Python 3.6.1中。 My Python 3.5.2 instead raises the expected TypeError: 'NoneType' object is not callable . 我的Python 3.5.2反而引发了预期的TypeError: 'NoneType' object is not callable And googling "must be an iterable, not map" finds no results at all. 谷歌搜索“必须是可迭代的,而不是地图”根本找不到任何结果。 So apparently this is something introduced just recently. 显然这是最近才引入的。

Is this just a Python bug? 这只是一个Python错误吗? Or is there some sense to this? 或者这有什么意义吗?

Update: Reported as bug now, as suggested. 更新:按照建议立即报告为错误

I'd consider this to be a bug. 我认为这是一个错误。 Here's the source that causes this exception: 这是导致此异常的源:

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

A disassembly of the python bytecode confirms it is using BUILD_TUPLE_UNPACK_WITH_CALL python字节码的反汇编确认它正在使用BUILD_TUPLE_UNPACK_WITH_CALL

The "bug" in the code above is it assumes any TypeError while _PyList_Extend ing the argument array means it wasn't an iterable, however __iter__ itself could raise a TypeError. 上面代码中的“bug”是假设任何 TypeError_PyList_Extend参数数组意味着它不是可迭代的,但__iter__本身可能引发TypeError。 It is rethrowing this exception 它正在重新抛出这个例外

I'd suggest opening a bug at https://bugs.python.org 我建议在https://bugs.python.org上打开一个错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM