python - Setting containers within a loop -
this question has answer here:
- do python loops work reference? 2 answers
dicta = {'a':1, 'b':2, 'c':3} prea = {} print hex(id(prea)) preb = {} print hex(id(preb)) dicts = [prea, preb] #<---- looks can't save this? d in dicts: print hex(id(d)) d = dicta print prea print preb output:
0x13348b0 0x13357f0 0x13348b0 0x13357f0 {} {} looks has same memory address when set prea or preb via variable 'd' , getting value prea or preb, it's if never set.
can explain whats going on here?
d set current dictionary @ iteration of each loop. setting d = dicta pointless because d change next dictionary first step.
Comments
Post a Comment