From: Steve S. <st...@sp...> - 2009-09-12 17:35:28
|
Hi Lenore... On Sep 12, 2009, at 8:38 AM, Lenore Horner wrote: > (Sorry, forgot to cc the list.) > > Hi Steve, > > You're saying the following? > a = 1 > b = a > b = 3 > Now print a would give 3? No... not really. Assignments in python just add references to existing objects. So... a=1 Makes the label 'a' refer to the integer object that represents the integer '1'. b=a Makes the label 'b' refer to the same object that 'a' refers to.... in this case it happnes to be an integer: '1' (but python doesn't really care about that). b=3 just makes 'b' now refer to the integer object '3', 'a' still refers to '1' as it did before. > > I could almost understand if the following were true. > air.acceleration = gravity > gravity = gravity + (1,0,1) > print acceleration gives (1,-9.8,1) > > That would mean the assignment was somehow permanent, which is to my > mind quite weird but potentially useful. This case is more subtle... gravity = gravity + (1,0,1) First the right hand side... "gravity + (1,0,1)" creates a *new* vector object. The assignment then makes the label 'gravity' refer to that new object. > > But it sounds like you're saying that assignment in python makes two > variables identical rather than giving the one on the left the current > value of the one on the right? That seems to me to be incredibly > awkward. How is one supposed to initialize several things identically > (no possibility of typos or slider misadjustments) and then let them > evolve differently? You can use the vector constructor to make multiple identical vectors: x = vector(1,2,3) y = vector(x) The 'vector(x)' creates a new, independent vector object that is initialized to have the same components as the existing vector object referred to by 'x'. As Bruce points out... check out the document: http://vpython.org/contents/experienced.html it has some great tips and may avert other problems ;-). Hope that's helpful.... -steve |