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

依然范特西╮ 提交于 2019-12-08 14:58:11

问题


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:

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

This happens in my Python 3.6.1. My Python 3.5.2 instead raises the expected 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? Or is there some sense to this?

Update: Reported as bug now, as suggested.


回答1:


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

A disassembly of the python bytecode confirms it is using BUILD_TUPLE_UNPACK_WITH_CALL

The "bug" in the code above is it assumes any TypeError while _PyList_Extending the argument array means it wasn't an iterable, however __iter__ itself could raise a TypeError. It is rethrowing this exception

I'd suggest opening a bug at https://bugs.python.org



来源:https://stackoverflow.com/questions/45363330/why-does-unpacking-this-map-object-print-must-be-an-iterable-not-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!