为什么解压这个地图对象会打印“必须是可迭代的,而不是地图"? [英] Why does unpacking this map object print "must be an iterable, not map"?

查看:82
本文介绍了为什么解压这个地图对象会打印“必须是可迭代的,而不是地图"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里发生了什么?

<预><代码>>>>列表(地图(拉姆达*x:x,*地图(无,'abc')))回溯(最近一次调用最后一次):文件<pyshell#52>",第 1 行,在 <module> 中列表(地图(拉姆达*x:x,*地图(无,'abc')))类型错误:* 之后的类型对象参数必须是可迭代的,而不是映射

忽略代码的无意义.这是关于错误消息,iterable, not map".地图 是可迭代的,不是吗?

如果我只用 str 替换 None,那么整个事情都可以正常工作:

<预><代码>>>>列表(地图(拉姆达*x:x,*地图(str,'abc')))[('a', 'b', 'c')]

所以现在 Python 对 map 毕竟没有任何问题.

这发生在我的 Python 3.6.1 中.我的 Python 3.5.2 反而引发了预期的 TypeError: 'NoneType' object is not callable.和谷歌搜索 "must be iterable, not map" 根本找不到结果.所以显然这是最近才推出的.

这只是一个 Python 错误吗?或者这有什么意义吗?

更新:按照建议现在报告为错误.>

解决方案

我认为这是一个错误.这是导致此异常的来源:

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

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

上面代码中的错误"是它假定 any TypeError_PyList_Extend 参数数组意味着它不是可迭代的,但是 __iter__ 本身可能会引发 TypeError.正在重新抛出这个异常

我建议在 https://bugs.python.org

打开一个错误

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.

解决方案

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

这篇关于为什么解压这个地图对象会打印“必须是可迭代的,而不是地图"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆