python - Sharing data between two lists -
first let me thought way data storage worked in python object there no need such things pointers. if pass piece of data function function has real piece of data. when exit out of function data passed in have been modified.
now working lists , thought if put same piece of data on 2 lists modifying in 1 place modify in other.
how can have 1 piece of data on two, or more, different lists? want change data in 1 place , have other change.
for example take following:
p = 9 d = [] f = [] d.append(p) f.append(p) print 'd',d print 'f',f p = 3 print 'd',d print 'f',f
when run output is:
d [9] f [9] d [9] f [9]
i second set of data 3 doesn't seem work. in thought process did go wrong? there implicit copy operation when putting data onto list?
first of all, integers (really, number objects) immutable. there no "modifying" "data". second of all, there in python notion of name binding. when do:
p = 9
two things happen: first, number object (9
) created; second, bound name p
.
when later do:
p = 3
it not, think, "modify" immutable number object created earlier. is, again, 2 things: first, new number object (3
) created; second, bound name p
.
pictorally:
(1a) 9 (1b) p --> 9 (2a) p --> 9 3 (2b) 9 p --> 3
Comments
Post a Comment