Oct-08-2019, 10:27 PM
as i understand it, slicing with [:] yields a new copy of the sequence, or if assigned to, replace the entire sequence (for a mutable sequence). but i have seen an odd effect when using [:] for both the source and target. the effect is that nothing happens. if i do
a=[1,2,3] b=a a=a[:] b.append(4) print(a)i would not see the 4 append to b. but if i do
a=[1,2,3] b=a a[:]=a[:] b.append(4) print(a)i do get the 4 as if a retained a reference to the same list.
Output:lt2a/forums /home/forums 55> cat t1
a=[1,2,3]
b=a
b=a[:]
b.append(4)
print(a)
lt2a/forums /home/forums 56> cat t2
a=[1,2,3]
b=a
a[:]=a[:]
b.append(4)
print(a)
lt2a/forums /home/forums 57> python3 t1
[1, 2, 3]
lt2a/forums /home/forums 58> python3 t2
[1, 2, 3, 4]
lt2a/forums /home/forums 59>
what's going on with a[:]=a[:]
?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.