これは、関数 2 で mylist を他のオブジェクトに割り当てたためです。
Python では、変更可能なオブジェクトを関数に渡す場合は参照渡しと呼ばれ、不変オブジェクトを渡す場合は値渡しと呼ばれます。
mylist = [1,2,3,4]
def changeme( mylist ):
print (id(mylist)) #prints 180902348
#at this point the local variable mylist points to [10,20,30]
mylist = [1,2,3,4]; #this reassignment changes the local mylist,
#now local mylist points to [1,2,3,4], while global mylist still points to [10,20,30]
print (id(mylist)) #prints 180938508 #it means both are different objects now
print ("Values inside the function: ", mylist)
return
mylist = [10,20,30];
changeme( mylist );
print ("Values outside the function: ", mylist)
最初の1つ:
def changeme( mylist ):
print (id(mylist)) #prints 180902348
mylist.append([1,2,3,4]); #by this it mean [10,20,30].append([1,2,3,4])
#mylist is just a label pointing to [10,20,30]
print (id(mylist)) #prints 180902348 , both point to same object
print ("Values inside the function: ", mylist)
return
mylist = [10,20,30];
changeme( mylist );
print ("Values outside the function: ", mylist)