From: Robert X. <nne...@gm...> - 2009-09-12 15:13:01
|
Vector objects are mutable, which means they get passed by reference instead of by value. It also means that statements like "a=b" do not copy the value, but they copy the reference (making a and b point to the same vector). So, a = vector(1,2,3) b = a b.x = 3 print a yields <3, 2, 3> as expected, because a and b refer to the same vector. The same thing happens with list objects in Python. Numbers, tuples and strings are immutable in Python, so they are passed by value. Robert 2009/9/12 Lenore Horner <lh...@si...> > (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? > > 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. > > 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? > > ----- > Testing: > Nope, that's not true. > >>> a = 1 > >>> print a > 1 > >>> b = a > >>> print a, b > 1 1 > >>> b = 3 > >>> print a, b > 1 3 > > Well maybe vectors behave differently than scalars. > Nope. > >>> a = (0,0,1) > >>> b = a > >>> print b > (0, 0, 1) > >>> b = (1,0,0) > >>> print a, b > (0, 0, 1) (1, 0, 0) > > Maybe a vector is different than a tuple. > Nope. > from visual import * > >>> a = vector(0,0,1) > >>> b = a > >>> b = vector(1,0,0) > >>> print a, b > <0, 0, 1> <1, 0, 0> > > But the above isn't quite what I'm doing in my code. Maybe component > assignment works differently than all other types of assignment. > Yup! > >>> a = vector(0,0,1) > >>> b = a > >>> c = a > >>> print a, b, c > <0, 0, 1> <0, 0, 1> <0, 0, 1> > >>> c.y = 5 > >>> print a, b, c > <0, 5, 1> <0, 5, 1> <0, 5, 1> > > Ok, that's just plain spooky. > > Can someone give me an example where that would be helpful? > > Now I have to go figure out how to dodge this bullet. > > Lenore > > > On Sep 12, 2009, at 06:36 , Steve Spicklemire wrote: > > > Hi Lenore, > > > > It's about python assignments. You are assigning the *same* > > acceleration vector to both objects, then changing the y component > > of the same vector. > No. I did not change the y component of the vector which was assigned > to both of them. > > > > Instead of assigning both accelerations to the same vector (gravity) > > try initializing them with different vectors: > > > > pixie.acceleration = vector(0,-9.8,0) > > air.acceleration = vector(0,-9.8,0) > > > > then when you fiddle with the y components, it will be the y > > components of different vector objects. > > > > That should work! > > -steve > > > > On Sep 12, 2009, at 7:26 AM, Lenore Horner wrote: > > > >> Sorry. I was in too big a hurry and forgot to paste the code. Here > >> it is. I've put arrows off to the right indicating the lines where > >> both air.acceleration and pixie.acceleration are being changed > >> despite > >> the fact that only air.acceleration appears in the code. I've > >> included all of the while loop. I don't see anywhere that I'm > >> reassigning pixie.acceleration from its original value of gravity but > >> it changes as soon as it hits those to statements (and not before). > >> > >> Thanks, > >> Lenore > >> > >> > >> angle = math.pi/4 > >> speed = 13.0 > >> > >> power = 2 > >> constant = 1 > >> > >> gravity = vector(0,-9.8,0) > >> > >> launch_velocity = > >> vector(speed*math.cos(angle),speed*math.sin(angle),0) > >> > >> pixie = sphere(pos=vector(-9,-3.55,0), radius=0.2, color=color.red) > >> pixie.velocity = launch_velocity > >> pixie.acceleration = gravity > >> > >> air = sphere(pos=vector(-9,-3.55,0.3), radius=0.15, color=color.blue) > >> air.velocity = launch_velocity > >> air.acceleration = gravity > >> > >> scene.mouse.getclick() # Don't launch until we're watching > >> > >> dt = 0.01 > >> while 1: > >> rate(100) > >> air.pos = air.pos + air.velocity*dt > >> if air.y < -3.55: > >> air.velocity = vector(0,0,0) > >> else: > >> air.velocity = air.velocity + air.acceleration*dt > >> air.acceleration.y = gravity.y - sign(air.velocity.y) * > >> constant * air.radius * air.velocity.y**power <----- > >> print "5", air.acceleration, pixie.acceleration > >> air.acceleration.x = -sign(air.velocity.x) * constant * > >> air.radius * air.velocity.x**power <------- > >> print "6", air.acceleration, pixie.acceleration > >> > >> pixie.pos = pixie.pos + pixie.velocity*dt > >> if pixie.y < -3.55: > >> pixie.velocity = vector(0,0,0) > >> else: > >> pixie.velocity = pixie.velocity + pixie.acceleration*dt > >> > >> > >> On Sep 11, 2009, at 18:20 , Bruce Sherwood wrote: > >> > >>> Can you explain the context of your question? I have no idea what > >>> program or > >>> documentation you're referring to. > >>> > >>> Bruce Sherwood > >>> > >>> Lenore Horner wrote: > >>>> Perhaps I'm just blind, but I can't figure out why the assignment > >>>> statements above print "5" and print "6" affect both acceleration > >>>> vectors, but they do. How do I change the acceleration vector of > >>>> one > >>>> object without touching the vector of other objects? > >>>> > >>>> Thanks, > >>>> Lenore > >>> > >>> > ------------------------------------------------------------------------------ > >>> 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 > >> > >> > >> > ------------------------------------------------------------------------------ > >> 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 > > > > > > ------------------------------------------------------------------------------ > 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 > |