Python
Consider the following code:
funs = [lambda: i for i in range(3)]
print([f() for f in funs])The result it [2,2,2].
funs = [lambda i=i:i for i in range(3)]
print([f() for f in funs])or
funs = (lambda: i for i in range(3))
print([f() for f in funs])The result is [1,2,3].
def f(n, lst=[]):
lst.append(n)
return lst
print(f(1), f(2))The result is [1,2], [1,2].
def f(n, lst=[]):
lst.append(n)
return lst*1 # or lst[:], list(lst), lst.copy()
print(f(1), f(2))The result is [1], [1,2].
For the shallow copy of a list, changing the top-level element does not change the original list. Only when you change the nested element, the original list will be changed too.