Saturday, 28 January 2017

Python: References, dictionaries and lists

I've just started writing with Python and still getting used with references and where they are and aren't used.




I've written the following code:



dummyList = self.getSprayLocation(heading, "left")
self.points['leftLeft'][0] = self.armLocations['leftX'] - self.mmToCoor(dummyList[0])
self.points['leftLeft'][1] = self.armLocations['leftY'] - self.mmToCoor(dummyList[1])
self.points['rightLeft'][0] = self.armLocations['leftX'] + self.mmToCoor(dummyList[0])
self.points['rightLeft'][1] = self.armLocations['leftY'] + self.mmToCoor(dummyList[1])
dummyList = self.getSprayLocation(heading, "mid")
print(self.points['leftLeft'][1])


self.points['leftMid'][0] = self.armLocations['midX'] - self.mmToCoor(dummyList[0])
self.points['leftMid'][1] = self.armLocations['midY'] - self.mmToCoor(dummyList[1])
self.points['rightMid'][0] = self.armLocations['midX'] + self.mmToCoor(dummyList[0])
self.points['rightMid'][1] = self.armLocations['midY'] + self.mmToCoor(dummyList[1])
print(self.points['leftLeft'][1])

dummyList = self.getSprayLocation(heading, "right")
self.points['leftRight'][0] = self.armLocations['rightX'] - self.mmToCoor(dummyList[0])
self.points['leftRight'][1] = self.armLocations['rightY'] - self.mmToCoor(dummyList[1])
self.points['rightRight'][0] = self.armLocations['rightX'] + self.mmToCoor(dummyList[0])

self.points['rightRight'][1] = self.armLocations['rightY'] + self.mmToCoor(dummyList[1])
print(self.points['leftLeft'][1])


It's in a Class, where 'points' is a dictionary containing a list:



coordinate = [0, 0]
points = {'leftLeft':coordinate, 'rightLeft':coordinate, 'leftMid':coordinate, 'rightMid':coordinate, 'leftRight':coordinate, 'rightRight':coordinate}



Note that after every block of code I print the ['leftLeft'][0] value. I expect this value not to change when I don't write to this key in the dictionary.



But when I run this code, this is the output



51.861101789
51.8611355556
51.8611192766


Which means the value is changed. In fact, all 'leftX' entries are the same and all 'rightX' entries are the same.




Now I think it has something to do with the references, but I haven't come up with a solution for this yet.



Thanks for your help!






Edit:
Thanks to JoshuaF I found that the reference was in the




coordinate = [0, 0]
points = {'leftLeft':coordinate, 'rightLeft':coordinate, 'leftMid':coordinate, 'rightMid':coordinate, 'leftRight':coordinate, 'rightRight':coordinate}


Block. 'coordinate' was the same 'coordinate' everywhere. The following fixes this:



coordinate = [0, 0]
points = {'leftLeft':coordinate[:], 'rightLeft':coordinate[:], 'leftMid':coordinate[:], 'rightMid':coordinate[:], 'leftRight':coordinate[:], 'rightRight':coordinate[:]}



I know the [:] has got something to do with references and lists. But what?






meaning of [:] in python



Python copy manual

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...