From: Arthur <aj...@ix...> - 2002-10-04 13:14:57
|
I think it worth supplementing Bruce's analysis with the solution to what was trying to be accomplished by rininitial = ball.pos I believe either: rinitial = ball.pos[:] or rinitial =copy(ball.pos) would get one to where one is trying to go. [:] returns a complete "slice" of the list. copy, of course, a copy of the list. In either case rinitial is no longer an additional name for ball.pos, but an object independant of it, which will not be modified upon the modification of ball.pos. I think I have this right. Corrections welcome. Art ----- Original Message ----- From: "Bruce Sherwood" <bas...@un...> To: "vpusers" <vis...@li...> Sent: Friday, October 04, 2002 8:16 AM Subject: [Visualpython-users] Re: strange output > A VPython user sent me a small program that behaved oddly, and I thought I > should share my analysis, since it involves an aspect of Python that is > subtle and may be missed in early study of the language. Fortunately it > rarely bites students writing computational physics programs! > > Early in the program the user said "rinitial = ball.pos" which means "assign > an additional name to ball.pos, so that in the future one can refer to the > ball's position either by the label (name) ball.pos or the label (name) > rinitial". You can have lots of names pasted onto the same thing. The puzzle > for the user was that later in the program (after moving the ball around), > printing rinitial showed the new value of ball.pos, not the initial value. > > This multiple labeling has consequences only with "mutable" objects such as > lists -- objects whose values can be changed in situ. For example, if you > say "a = 5" and then "b = a", and then say "a = 7", b will still have the > value 5. When you assign 7 to a, you create a new object ("7") and attach > the label "a" to it, so b continues to be a label for the object "5". > > But with "rinitial = ball.pos", you can actually change ball.pos without > creating a new object. For example, you might say "ball.pos.y = 7" in which > case the 2nd element in the ball.pos list has changed (without affecting the > other two elements). Since rinitial is a label for ball.pos, printing > rinitial shows you the new value of ball.pos. > > A list such as [1,2,3] is mutable. The Visual vectors are mutable. A 'tuple' > such as (1,2,3) is not mutable. Constants such as 3 or pi or a string such > as 'cat' are not mutable. > > Needless to say, it took me a while to pay attention to "mutability" as an > important property of Python objects! > > Bruce > > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > Visualpython-users mailing list > Vis...@li... > https://lists.sourceforge.net/lists/listinfo/visualpython-users > |