在中lol(a),该行a=[]绑定a到新列表。在此行之后,a不再与绑定到同一列表k。因此,您实际上在函数中修改的是一个新列表,而不是您在参数中收到的列表。有关更多信息,请参阅docs。如果您想k成为[1],则可以改成这样的形式:def lol(a): a[:] = [1] return ak = [2, 3]lol(k) # now k is equal to [1]
检查嵌入式注释。def lol(a): # a and k are pointing to same memory location a=[] # Now, new list is created, pointing to different memory address. a.append(1)return a # returning new list k = [2, 3] # New list k is createdlol(k)如果打印k,将得到[2,3]。它根本没有被lol()更改。