Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我偶然发生了这种奇怪的行为:
>>> a = [] >>> a[:] = ['potato', a] >>> print a ['potato', [...]] >>> print list(a) ['potato', ['potato', [...]]]
通过什么机制调用list(a)在其自身的字符串表示中展开一层递归?
list(a)
仅当...项目包含自身时才显示 - 即相同的对象。 list(a)制作列表的副本,因此内部a不是同一个对象。它只显示...何时到达“a inside a”,而不是“a inside list(a)”。
...
a
list()做一个浅拷贝。外部列表不再是与其包含的列表相同的对象。它按照您的预期打印。
list()