From: Craig S. <cra...@ma...> - 2009-09-14 17:29:53
|
Hi Bruce, Ah, you're getting into the strangeness of objects. The expression 3*A creates a new object and assigns it to the name A you're using. That won't be the same object assigned to axis. In order to do the updates, assuming vectors are mutable, the code should be A.x = 3*A.x A.y = 3*A.y A.z = 3*A.z I tried this, however, and it also didn't work because of axis=A behaving like axis=vector(A); that is, arrow creates a copy of the passed in object. Creating a copy of the vector is a design decision that's appropriate for the intended audience of VPython. Doing it the other way, using only the object reference, would cause all kinds of side effects that would be difficult to debug. This is, however, inconsistent with the general behavior of Python. If I wrote the code A = vector(0,1,0) b = arrow(pos=(0,0,0)) c = arrow(pos=(0,1,0)) b.axis = A c.axis = A A.x = 1 As a Python programmer, I expect b and c to use the same vector object for their axis, and changes to that object (A.x = 1), should update the axis for both arrows referred to by b and c. If I intended to treat b and c differently, but to start with the same axis, I would explicitly write b.axis = vector(A) c.axis = vector(A) This all gets back to the intended audience, but making it clear where VPython might not behave exactly like Python in all cases. I can work around each approach, I just need to know my limits. Craig On Sep 14, 2009, at 10:38 AM, Bruce Sherwood wrote: > Thanks for the additional background, Craig. It's good to have a > card-carrying > computer scientist commenting on the issues! > > Your point about updating a bunch of Visual objects however doesn't > work, > because the setters for Visual vector attributes like pos or axis > make new > vectors. This explains for example why you can write as a shorthand > pos=(1,0,0) > instead of pos=vector(1,0,0); a vector is constructed out of the > triple. > > Here is a routine that shows the (non)effect: > > A = vector(0,1,0) > arrow(pos=(0,0,0), axis=A) > arrow(pos=(1,0,0), axis=A) > scene.mouse.getclick() > A = 3*A > > After clicking, the arrows don't get longer, because axis=A is > basically > axis=vector(A). > > In any case, it would be a rare circumstance where you wanted some > vector > attribute of lots of objects to be the same, and to change together. > > Bruce Sherwood > > Craig Struble wrote: >> It should be pointed out that many object oriented languages share >> this behavior. Java behaves the same way, for example. Many others >> have pointed out the difficulties in copying objects (deep vs. >> shallow >> copies) and bigger problems for copying arise when there are circular >> references, which are common for complex data structures (e.g. >> general >> graphs). >> >> The object reference behavior is useful when implementing some kinds >> of data structures when you need a "trailing pointer" for updates. >> Lots of linked list implementations use this approach. It's atypical >> to do this in Python since it has a very rich collection of data >> structures already implemented, but it can be useful. >> >> I would guess in VPython, this behavior could make it easier to >> update >> the positions or movements of a large group of visual objects. If >> they >> all refer to the same point or vector, change that vector and all the >> objects change accordingly. It would be nice to make a single update >> instead of having to looping over each object, which would probably >> be >> slower. >> >> Craig >> > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 > 30-Day > trial. Simplify your report design, integration and deployment - and > focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > Visualpython-users mailing list > Vis...@li... > https://lists.sourceforge.net/lists/listinfo/visualpython-users -- Craig A. Struble, Ph.D. | 369 Cudahy Hall | Marquette University Associate Professor of Computer Science | (414)288-3783 Director, Master of Bioinformatics Program | (414)288-5472 (fax) http://www.mscs.mu.edu/~cstruble | cra...@ma... |