2016年3月23日 星期三

[python] python 3.4 in place change and is (原地修改 和 判斷是否為同一個物件)

#example code

a=b=c=[0,1,2] # a,b,c assign the same address of list
b=b+[3,4,5] # b will create new list to store the new list
c+=[97,98,99] # c is in place change

print(a,b,c)
print (a is b, a is c ) # use 'is' to check is the same object or not


output
[0, 1, 2, 97, 98, 99] [0, 1, 2, 3, 4, 5] [0, 1, 2, 97, 98, 99]
False True
[Finished in 0.0s]

沒有留言:

張貼留言