0

what is the meaning of b=a when a is a list?see my example please.

a=[1,2,3]b=a after a.append[4] or other operater. a,b is always the same.

15th Oct 2017, 3:16 AM
lwluowei
4 odpowiedzi
+ 2
a=[1,2,3]; b=a; a.push(4) alert(a) alert(b) js also Ans : This is Reference
15th Oct 2017, 3:40 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 2
Yes as he pointed/elaborate out You have to do like this a=[1,2,3] b=a[:] #slice with empty will return copy form a.append(4)
15th Oct 2017, 4:18 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
0
Look up the difference between reference types and value types. Reference types only store the reference of the object, such as a list, so the value of the variable is just a link to the actual object. Multiple variables just means multiple links to the same object. You need to do a deep copy to make a new list if you want to keep the original one separate.
15th Oct 2017, 3:52 AM
Jesse Bayliss
Jesse Bayliss - avatar
0
thanks for your help.~( ̄▽ ̄~)~The fact is that if i can use a[list] to do what i want ,why i use b instead?i will look up the usage of reference type.some example is also very helpful.Love you guys
15th Oct 2017, 4:26 AM
lwluowei