lst = iter([1, 2, 3])
print([*lst]) # >>> [1, 2, 3]
print([*lst]) # >>> []
这是解包的预期行为吗?我会假设原始数据在解包时不会被修改,只是简单地制作了一个副本?
编辑:
如果是这样,背后的原因是什么?
最佳答案
来自glossary entry for iterators (我的重点):
A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.
关于python - Doubley 在 Python 3 中解压一个可迭代对象会删除可迭代对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52009624/