my_list = [1, 2]
def f():
print my_list
yield 11
print my_list
yield 22
print my_list
my_list[:] = f()
print "finally ",
print my_list
输出:
[1, 2]
[1, 2]
[1, 2]
finally [11, 22]
我的预期是:
[1, 2]
[11, 2]
[11, 22]
finally [11, 22]
有人曾经告诉我切片分配已经到位。显然不是。有没有一种优雅的方式来实现它?