From: Jonathan B. <jbr...@ea...> - 2004-08-22 21:06:43
|
On Sun, 2004-08-22 at 16:38, Bruce Sherwood wrote: > I ran Jonathan's test routine in pre-Boost and Boost versions of > VPython. In the pre-Boost version the behavior is NOT what Gary and > Jonathan describe. In the pre-Boost version the two forms of the update > statement work exactly the same in Jonathan's test routine. The original > Visual deliberately did something like this: > > sphere(pos=a) was treated as though it were written as > sphere(pos=vector(a)) Current VPython does this too for all of its built-in attributes. The problem stems from the use of user-defined attributes, for which there is no way to force a copy that will work for all Python data types (copy.copy() is close, but doesn't work for everything). Old visual.vector did not provide the in-place operators (+=, *=, and friends), which forced the interpreter to emulate them incorrectly. Python programmers must be aware that all Python variables are references. Consider this program, which also incorrectly assumes that '=' makes a copy: from visual import * spheres = [] velocity = vector(0,0,0) spheres.append( sphere( v=velocity)) spheres.append( sphere( v=velocity)) twice = 2 while twice: print "before iteration:" for i in spheres: print i.v ctr = 0 for i in spheres: print ctr ctr += 1 print "before:", i.v i.v.x += 1 print "after", i.v twice -= 1 -Jonathan |