From: Lenore H. <lh...@si...> - 2009-09-11 18:16:19
|
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 |
From: Robert X. <nne...@gm...> - 2009-09-11 18:35:20
|
What code are you using? 2009/9/11 Lenore Horner <lh...@si...> > 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 > |
From: Bruce S. <Bru...@nc...> - 2009-09-11 23:20:25
|
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 |
From: Lenore H. <lh...@si...> - 2009-09-12 11:26:46
|
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 |
From: Steve S. <st...@sp...> - 2009-09-12 12:04:00
|
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. 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 |
From: Lenore H. <lh...@si...> - 2009-09-12 12:38:31
|
(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 > |
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 > |
From: Lenore H. <lh...@si...> - 2009-09-12 16:23:50
|
So a vector is a list which also has strange properties. I still want to know why it is more useful to have two names for the same thing than it is to copy the values from one thing to another thing. Granted, I have since found out I can work around this problem with the following clumsy process (the least clumsy of the three I have found). >>> from visual import * >>> a = vector(0,0,1) >>> b = vector(a.x,a.y,a.z) >>> print a,b <0, 0, 1> <0, 0, 1> >>> b.x=1 >>> print a,b <0, 0, 1> <1, 0, 1> Lenore On Sep 12, 2009, at 10:12 , Robert Xiao wrote: > 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 > > ------------------------------------------------------------------------------ > 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 |
From: Bruce S. <Bru...@nc...> - 2009-09-12 17:17:13
|
It may be useful to read the discussion "How Python is different from languages you may know" found in the documentation section of vpython.org. The most common trap for experienced programmers who are new to Python is precisely this issue. Bruce Sherwood Lenore Horner wrote: > So a vector is a list which also has strange properties. I still want > to know why it is more useful to have two names for the same thing than > it is to copy the values from one thing to another thing. > > Granted, I have since found out I can work around this problem with the > following clumsy process (the least clumsy of the three I have found). > >>> from visual import * > >>> a = vector(0,0,1) > >>> b = vector(a.x,a.y,a.z) > >>> print a,b > <0, 0, 1> <0, 0, 1> > >>> b.x=1 > >>> print a,b > <0, 0, 1> <1, 0, 1> > > Lenore > > > On Sep 12, 2009, at 10:12 , Robert Xiao wrote: > >> 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... <mailto: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... >> <mailto: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... >> <mailto: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... >> <mailto: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... >> <mailto: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 |
From: Craig S. <cra...@ma...> - 2009-09-14 14:10:28
|
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 On Sep 12, 2009, at 12:17 PM, Bruce Sherwood wrote: > It may be useful to read the discussion "How Python is different > from languages > you may know" found in the documentation section of vpython.org. The > most common > trap for experienced programmers who are new to Python is precisely > this issue. > > Bruce Sherwood > > Lenore Horner wrote: >> So a vector is a list which also has strange properties. I still >> want >> to know why it is more useful to have two names for the same thing >> than >> it is to copy the values from one thing to another thing. >> >> Granted, I have since found out I can work around this problem with >> the >> following clumsy process (the least clumsy of the three I have >> found). >>>>> from visual import * >>>>> a = vector(0,0,1) >>>>> b = vector(a.x,a.y,a.z) >>>>> print a,b >> <0, 0, 1> <0, 0, 1> >>>>> b.x=1 >>>>> print a,b >> <0, 0, 1> <1, 0, 1> >> >> Lenore >> >> >> On Sep 12, 2009, at 10:12 , Robert Xiao wrote: >> >>> 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... <mailto: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... >>> <mailto: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... >>> <mailto: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... >>> <mailto: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... >>> <mailto: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 -- 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... |
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 |
From: Lenore H. <lh...@si...> - 2009-09-12 17:59:12
|
> http://vpython.org/contents/experienced.html I'm sorry to say that it was entirely unenlightening. The only relevant statement seems to be this "Making a second, independent copy of an object is a fairly unusual thing to do in Python, but there exist special methods for doing this." But there is zilch explanation of why this is so. I repeat: why ever would I want two identical objects that _must remain so forever_ ? I completely fail to grasp the utility of this concept. I cannot think of a single thing that I can accomplish with two identical objects that I cannot accomplish with only one of them. Sorry to be so boneheaded about this, but I just don't get it. Lenore |
From: Bruce S. <Bru...@nc...> - 2009-09-12 19:07:11
|
None of us readers of this mailing list participated in the creation of Python, so I doubt that anyone here can tell you why Python is built that way, nor argue the merits of the case. Nor was the intent of the experienced.html article to explain this behavior, only to alert you to it. As for utility of this behavior: It's a bit of a stretch for me, but I can imagine a situation where you want to refer to some very long name by using a short name, as in s = the_sphere_whose_outer_surface_represents_Earth = sphere() s.pos = vector(10,20,30) I tried a little googling to find some history and found this laconic comment: "It is not weird in Python. Implicit copies are never made, partly because it's very hard to implement it correctly for every object." Also, http://merd.sourceforge.net/inoutness.html seems to summarize these issues for diverse programming languages. Bruce Sherwood Lenore Horner wrote: > >> http://vpython.org/contents/experienced.html > > I'm sorry to say that it was entirely unenlightening. The only relevant > statement seems to be this "Making a second, independent copy of an > object is a fairly unusual thing to do in Python, but there exist > special methods for doing this." But there is zilch explanation of why > this is so. I repeat: why ever would I want two identical objects that > _must remain so forever_ ? I completely fail to grasp the utility of > this concept. I cannot think of a single thing that I can accomplish > with two identical objects that I cannot accomplish with only one of them. > > Sorry to be so boneheaded about this, but I just don't get it. > > Lenore |
From: Jacob S. <sch...@ro...> - 2009-09-12 21:05:01
|
There are many reasons for this type of behavior. And there are two sides to this coin. A) Why would you want the names to be references of an object? Imagine you want to pass a custom object into a function. Python doesn't know how to pass it in by value -> it is a distinct instance. For example -> you make a Rational class that you can use for rational number arithmetic. What are you supposed to do when you add two of them together? Change the first one? Change the second one? Make a new one? Alright. That's simple enough. Make a new one. But what if you go deeper, make more complex functions, more complex objects? You can't make a new one every time you want to change something. That's time consuming AND memory consuming. B) Why can't you just copy and pass everything by value? How do you copy an object? Maybe for integers, it is a simple matter of copying the bits into a new object and you're done. How do you copy a list? Do you copy the references of all the items in the list to the new list, or do you try to copy recursively all the items in the list? How do you copy a custom object? What if that object is based on dynamic data? Each object should have a unique identifier, right? But that sometimes defeats the point of the copy. You want the two objects to be identical. Python was written in such a way that it balances these arguments. Tuples and lists are separate entities because they behave differently and are used in the situations for which they are best suited. Assignment just creates a new reference because it is the cheapest quickest way to do it, and in most situations it works. On Sat, 2009-09-12 at 15:07 -0400, Bruce Sherwood wrote: > None of us readers of this mailing list participated in the creation of Python, > so I doubt that anyone here can tell you why Python is built that way, nor argue > the merits of the case. Nor was the intent of the experienced.html article to > explain this behavior, only to alert you to it. > > As for utility of this behavior: It's a bit of a stretch for me, but I can > imagine a situation where you want to refer to some very long name by using a > short name, as in > > s = the_sphere_whose_outer_surface_represents_Earth = sphere() > s.pos = vector(10,20,30) > > I tried a little googling to find some history and found this laconic comment: > > "It is not weird in Python. Implicit copies are never made, partly > because it's very hard to implement it correctly for every object." > > Also, http://merd.sourceforge.net/inoutness.html seems to summarize these issues > for diverse programming languages. > > Bruce Sherwood > > Lenore Horner wrote: > > > >> http://vpython.org/contents/experienced.html > > > > I'm sorry to say that it was entirely unenlightening. The only relevant > > statement seems to be this "Making a second, independent copy of an > > object is a fairly unusual thing to do in Python, but there exist > > special methods for doing this." But there is zilch explanation of why > > this is so. I repeat: why ever would I want two identical objects that > > _must remain so forever_ ? I completely fail to grasp the utility of > > this concept. I cannot think of a single thing that I can accomplish > > with two identical objects that I cannot accomplish with only one of them. > > > > Sorry to be so boneheaded about this, but I just don't get it. > > > > 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 > |
From: Tony R. <To...@br...> - 2009-09-13 00:03:27
|
Additionally, many objects that I'm used to working with contain open streams, database links, and other singular, resource type references. How does one copy these by value? I am most experienced with php, and since 5.x, all objects are passed by reference; one must use the "copy" keyword which would invoke the __copy__() special method. I used the pass stuff around by reference all the time in php, so python makes perfect sense to me. Also, pretty much all systems have a "copy on write" policy anyway, so your not really passing by value until you modify the new reference. Most new references created are read only, and of they are not, I like the python way of making it obvious in the code by explicity creating a new object, as in the b = vector(a) examples. On Sep 12, 2009, at 4:06 PM, "Jacob Schmidt" <schmidjw@rose- hulman.edu> wrote: > There are many reasons for this type of behavior. > > And there are two sides to this coin. > > A) Why would you want the names to be references of an object? > > Imagine you want to pass a custom object into a function. Python > doesn't > know how to pass it in by value -> it is a distinct instance. > > For example -> you make a Rational class that you can use for rational > number arithmetic. What are you supposed to do when you add two of > them > together? Change the first one? Change the second one? Make a new one? > Alright. That's simple enough. Make a new one. But what if you go > deeper, make more complex functions, more complex objects? You can't > make a new one every time you want to change something. That's time > consuming AND memory consuming. > > > B) Why can't you just copy and pass everything by value? > > How do you copy an object? Maybe for integers, it is a simple matter > of > copying the bits into a new object and you're done. How do you copy a > list? Do you copy the references of all the items in the list to the > new > list, or do you try to copy recursively all the items in the list? How > do you copy a custom object? What if that object is based on dynamic > data? Each object should have a unique identifier, right? But that > sometimes defeats the point of the copy. You want the two objects to > be > identical. > > Python was written in such a way that it balances these arguments. > Tuples and lists are separate entities because they behave differently > and are used in the situations for which they are best suited. > > Assignment just creates a new reference because it is the cheapest > quickest way to do it, and in most situations it works. > > > > On Sat, 2009-09-12 at 15:07 -0400, Bruce Sherwood wrote: >> None of us readers of this mailing list participated in the >> creation of Python, >> so I doubt that anyone here can tell you why Python is built that >> way, nor argue >> the merits of the case. Nor was the intent of the experienced.html >> article to >> explain this behavior, only to alert you to it. >> >> As for utility of this behavior: It's a bit of a stretch for me, >> but I can >> imagine a situation where you want to refer to some very long name >> by using a >> short name, as in >> >> s = the_sphere_whose_outer_surface_represents_Earth = sphere() >> s.pos = vector(10,20,30) >> >> I tried a little googling to find some history and found this >> laconic comment: >> >> "It is not weird in Python. Implicit copies are never made, partly >> because it's very hard to implement it correctly for every object." >> >> Also, http://merd.sourceforge.net/inoutness.html seems to summarize >> these issues >> for diverse programming languages. >> >> Bruce Sherwood >> >> Lenore Horner wrote: >>> >>>> http://vpython.org/contents/experienced.html >>> >>> I'm sorry to say that it was entirely unenlightening. The only >>> relevant >>> statement seems to be this "Making a second, independent copy of an >>> object is a fairly unusual thing to do in Python, but there exist >>> special methods for doing this." But there is zilch explanation >>> of why >>> this is so. I repeat: why ever would I want two identical >>> objects that >>> _must remain so forever_ ? I completely fail to grasp the utility >>> of >>> this concept. I cannot think of a single thing that I can >>> accomplish >>> with two identical objects that I cannot accomplish with only one >>> of them. >>> >>> Sorry to be so boneheaded about this, but I just don't get it. >>> >>> 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 > > Tony Risinger Application Development Specialist Tel: 507-535-7563 | Fax: 507-292-5747 | Toll Free: 866-241-0639 To...@Br... http://www.brokerbin.com/ CONFIDENTIAL INFORMATION: This electronic mail message and any attached files contain information intended for the exclusive use of the specific individual(s) or entity(s) to whom it is addressed and may contain information that is propriety, privileged or confidential or otherwise exempt from disclosure. If you are not the intended recipient, please notify the sender immediately, by reply electronic mail or by telephone, of any unintended recipients so we may correct our records. Also, delete the original electronic mail and any attachments without making any copies of the electronic mail message or attachments. |
From: Bruce S. <Bru...@nc...> - 2009-09-13 14:09:03
|
Thanks much to both of you for posting these comments on references. Crossing paths, I also posted a comment about this with a pointer to some discussion on the web. Bruce Sherwood Tony Risinger wrote: > Additionally, many objects that I'm used to working with contain open > streams, database links, and other singular, resource type > references. How does one copy these by value? > > I am most experienced with php, and since 5.x, all objects are passed > by reference; one must use the "copy" keyword which would invoke the > __copy__() special method. > > I used the pass stuff around by reference all the time in php, so > python makes perfect sense to me. Also, pretty much all systems have > a "copy on write" policy anyway, so your not really passing by value > until you modify the new reference. Most new references created are > read only, and of they are not, I like the python way of making it > obvious in the code by explicity creating a new object, as in the > > b = vector(a) > > examples. > > On Sep 12, 2009, at 4:06 PM, "Jacob Schmidt" <schmidjw@rose- > hulman.edu> wrote: > >> There are many reasons for this type of behavior. >> >> And there are two sides to this coin. >> >> A) Why would you want the names to be references of an object? >> >> Imagine you want to pass a custom object into a function. Python >> doesn't >> know how to pass it in by value -> it is a distinct instance. >> >> For example -> you make a Rational class that you can use for rational >> number arithmetic. What are you supposed to do when you add two of >> them >> together? Change the first one? Change the second one? Make a new one? >> Alright. That's simple enough. Make a new one. But what if you go >> deeper, make more complex functions, more complex objects? You can't >> make a new one every time you want to change something. That's time >> consuming AND memory consuming. >> >> >> B) Why can't you just copy and pass everything by value? >> >> How do you copy an object? Maybe for integers, it is a simple matter >> of >> copying the bits into a new object and you're done. How do you copy a >> list? Do you copy the references of all the items in the list to the >> new >> list, or do you try to copy recursively all the items in the list? How >> do you copy a custom object? What if that object is based on dynamic >> data? Each object should have a unique identifier, right? But that >> sometimes defeats the point of the copy. You want the two objects to >> be >> identical. >> >> Python was written in such a way that it balances these arguments. >> Tuples and lists are separate entities because they behave differently >> and are used in the situations for which they are best suited. >> >> Assignment just creates a new reference because it is the cheapest >> quickest way to do it, and in most situations it works. >> >> >> >> On Sat, 2009-09-12 at 15:07 -0400, Bruce Sherwood wrote: >>> None of us readers of this mailing list participated in the >>> creation of Python, >>> so I doubt that anyone here can tell you why Python is built that >>> way, nor argue >>> the merits of the case. Nor was the intent of the experienced.html >>> article to >>> explain this behavior, only to alert you to it. >>> >>> As for utility of this behavior: It's a bit of a stretch for me, >>> but I can >>> imagine a situation where you want to refer to some very long name >>> by using a >>> short name, as in >>> >>> s = the_sphere_whose_outer_surface_represents_Earth = sphere() >>> s.pos = vector(10,20,30) >>> >>> I tried a little googling to find some history and found this >>> laconic comment: >>> >>> "It is not weird in Python. Implicit copies are never made, partly >>> because it's very hard to implement it correctly for every object." >>> >>> Also, http://merd.sourceforge.net/inoutness.html seems to summarize >>> these issues >>> for diverse programming languages. >>> >>> Bruce Sherwood >>> >>> Lenore Horner wrote: >>>>> http://vpython.org/contents/experienced.html >>>> I'm sorry to say that it was entirely unenlightening. The only >>>> relevant >>>> statement seems to be this "Making a second, independent copy of an >>>> object is a fairly unusual thing to do in Python, but there exist >>>> special methods for doing this." But there is zilch explanation >>>> of why >>>> this is so. I repeat: why ever would I want two identical >>>> objects that >>>> _must remain so forever_ ? I completely fail to grasp the utility >>>> of >>>> this concept. I cannot think of a single thing that I can >>>> accomplish >>>> with two identical objects that I cannot accomplish with only one >>>> of them. >>>> >>>> Sorry to be so boneheaded about this, but I just don't get it. >>>> >>>> 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 >> >> > > > Tony Risinger > Application Development Specialist > Tel: 507-535-7563 | Fax: 507-292-5747 | Toll Free: 866-241-0639 > > To...@Br... > > http://www.brokerbin.com/ > > CONFIDENTIAL INFORMATION: This electronic mail message and any attached files contain information intended for the exclusive use of the specific individual(s) or entity(s) to whom it is addressed and may contain information that is propriety, privileged or confidential or otherwise exempt from disclosure. If you are not the intended recipient, please notify the sender immediately, by reply electronic mail or by telephone, of any unintended recipients so we may correct our records. Also, delete the original electronic mail and any attachments without making any copies of the electronic mail message or attachments. > > ------------------------------------------------------------------------------ > 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 |
From: Tony R. <To...@br...> - 2009-09-13 14:43:36
|
This is my last measly nickel on the matter :-) : On Sep 12, 2009, at 11:09 PM, "Lenore Horner" <lh...@si...> wrote: > > On Sep 12, 2009, at 19:03 , Tony Risinger wrote: > >> Additionally, many objects that I'm used to working with contain open >> streams, database links, and other singular, resource type >> references. How does one copy these by value? > Sorry. Still baffled. How do you do anything at all with it if you > don't know what it is? However complicated your resource type, it is > at bottom still a string of 0s and 1s is it not? If you cannot read > the sequence to copy it somewhere else, then how can you read it to do > anything else with it? > > If you do > streamB = streamA > where streamB and streamA are now pointing at the same memory > location, > what can you now do that you couldn't do with streamA alone? Sure maybe some ones and zeros eventually, but maybe on a remote machine in a land far far away. A database link cannot be copied; you would either need too reuse the open link, or open a new connection consuming a second slot on the database. > Can someone give me an example where it is necessary to have two names > for the same thing. You are right you could do everything with stream b that you could do with a, since they are the same object. I can't think of a situation where you need to use multiple names in the same scope beyond what Bruce said about simplifying a long name, but, at least how I understand it, that's not the "pass by" part in pass by reference/ value. When you "pass" an object to a method/function, you are essentially giving it a local name/alias inside that method and it's scope. It is now called stream b, even though it's still stream a too, and still working on the same actual object instance. Hence you "passed by reference". > After cudgeling my brain over this for most of the day, the only > possible explanation I have come up with is that you want to scatter > the references around in the memory so that there is always a > reference near enough your other stuff to be read into the register > easily at the same time. In most (prob all) production apps Ive worked on, it's very rare that you would ever need a full copy; usually you create an object and pass it around, doing operations on it, extracting information from it, and so on. It seems that if you're routinely deep copying objects, there might be a better way to think about your problem, but I could be wrong. I'm not super savvy with python, but on other lists I've seen some good explanations. For example, I once read that in python there really isn't "variables"... there are only objects and name bindings/ name spaces. Once you get pass that Python isn't language X, it really does work quite consistently. > > >> >> I am most experienced with php, and since 5.x, all objects are passed >> by reference; one must use the "copy" keyword which would invoke the >> __copy__() special method. >> >> I used the pass stuff around by reference all the time in php, so >> python makes perfect sense to me. Also, pretty much all systems have >> a "copy on write" policy anyway, so your not really passing by value >> until you modify the new reference. > Fine - as long as when I modify the new reference it doesn't change > the original - and vice versa. >> Most new references created are >> read only, and of they are not, I like the python way of making it >> obvious in the code by explicity creating a new object, as in the >> >> b = vector(a) >> >> examples. > Whereas almost everything I do it makes sense only to pass by value > and I expect passage by reference to have a different syntax than > passage by value. Python, however, uses identical syntax whose effect > is object dependent. I would at the very least expect that > a=b > would have the same effect (or complain that the command is invalid) > regardless of what types a and b might be. > >> >> On Sep 12, 2009, at 4:06 PM, "Jacob Schmidt" <schmidjw@rose- >> hulman.edu> wrote: >> >>> There are many reasons for this type of behavior. >>> >>> And there are two sides to this coin. >>> >>> A) Why would you want the names to be references of an object? >>> >>> Imagine you want to pass a custom object into a function. Python >>> doesn't >>> know how to pass it in by value -> it is a distinct instance. >>> >>> For example -> you make a Rational class that you can use for >>> rational >>> number arithmetic. What are you supposed to do when you add two of >>> them >>> together? Change the first one? Change the second one? Make a new >>> one? >>> Alright. That's simple enough. Make a new one. But what if you go >>> deeper, make more complex functions, more complex objects? You can't >>> make a new one every time you want to change something. That's time >>> consuming AND memory consuming. >>> >>> >>> B) Why can't you just copy and pass everything by value? >>> >>> How do you copy an object? Maybe for integers, it is a simple matter >>> of >>> copying the bits into a new object and you're done. How do you >>> copy a >>> list? Do you copy the references of all the items in the list to the >>> new >>> list, or do you try to copy recursively all the items in the list? >>> How >>> do you copy a custom object? What if that object is based on dynamic >>> data? Each object should have a unique identifier, right? But that >>> sometimes defeats the point of the copy. You want the two objects to >>> be >>> identical. >>> >>> Python was written in such a way that it balances these arguments. >>> Tuples and lists are separate entities because they behave >>> differently >>> and are used in the situations for which they are best suited. >>> >>> Assignment just creates a new reference because it is the cheapest >>> quickest way to do it, and in most situations it works. >>> >>> >>> >>> On Sat, 2009-09-12 at 15:07 -0400, Bruce Sherwood wrote: >>>> None of us readers of this mailing list participated in the >>>> creation of Python, >>>> so I doubt that anyone here can tell you why Python is built that >>>> way, nor argue >>>> the merits of the case. Nor was the intent of the experienced.html >>>> article to >>>> explain this behavior, only to alert you to it. >>>> >>>> As for utility of this behavior: It's a bit of a stretch for me, >>>> but I can >>>> imagine a situation where you want to refer to some very long name >>>> by using a >>>> short name, as in >>>> >>>> s = the_sphere_whose_outer_surface_represents_Earth = sphere() >>>> s.pos = vector(10,20,30) >>>> >>>> I tried a little googling to find some history and found this >>>> laconic comment: >>>> >>>> "It is not weird in Python. Implicit copies are never made, partly >>>> because it's very hard to implement it correctly for every object." >>>> >>>> Also, http://merd.sourceforge.net/inoutness.html seems to summarize >>>> these issues >>>> for diverse programming languages. >>>> >>>> Bruce Sherwood >>>> >>>> Lenore Horner wrote: >>>>> >>>>>> http://vpython.org/contents/experienced.html >>>>> >>>>> I'm sorry to say that it was entirely unenlightening. The only >>>>> relevant >>>>> statement seems to be this "Making a second, independent copy of >>>>> an >>>>> object is a fairly unusual thing to do in Python, but there exist >>>>> special methods for doing this." But there is zilch explanation >>>>> of why >>>>> this is so. I repeat: why ever would I want two identical >>>>> objects that >>>>> _must remain so forever_ ? I completely fail to grasp the utility >>>>> of >>>>> this concept. I cannot think of a single thing that I can >>>>> accomplish >>>>> with two identical objects that I cannot accomplish with only one >>>>> of them. >>>>> >>>>> Sorry to be so boneheaded about this, but I just don't get it. >>>>> >>>>> 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 >>> >>> >> >> >> Tony Risinger >> Application Development Specialist >> Tel: 507-535-7563 | Fax: 507-292-5747 | Toll Free: 866-241-0639 >> >> To...@Br... >> >> http://www.brokerbin.com/ >> >> CONFIDENTIAL INFORMATION: This electronic mail message and any >> attached files contain information intended for the exclusive use of >> the specific individual(s) or entity(s) to whom it is addressed and >> may contain information that is propriety, privileged or >> confidential or otherwise exempt from disclosure. If you are not the >> intended recipient, please notify the sender immediately, by reply >> electronic mail or by telephone, of any unintended recipients so we >> may correct our records. Also, delete the original electronic mail >> and any attachments without making any copies of the electronic mail >> message or attachments. >> >> --- >> --- >> --- >> --------------------------------------------------------------------- >> 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 > > Tony Risinger Application Development Specialist Tel: 507-535-7563 | Fax: 507-292-5747 | Toll Free: 866-241-0639 To...@Br... http://www.brokerbin.com/ CONFIDENTIAL INFORMATION: This electronic mail message and any attached files contain information intended for the exclusive use of the specific individual(s) or entity(s) to whom it is addressed and may contain information that is propriety, privileged or confidential or otherwise exempt from disclosure. If you are not the intended recipient, please notify the sender immediately, by reply electronic mail or by telephone, of any unintended recipients so we may correct our records. Also, delete the original electronic mail and any attachments without making any copies of the electronic mail message or attachments. |
From: Bruce S. <Bru...@nc...> - 2009-09-14 15:38:40
|
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 > |
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... |
From: Bruce S. <Bru...@nc...> - 2009-09-14 22:49:33
|
Right on all counts. I shouldn't try being analytical when I've got the flu! Bruce Sherwood Craig Struble wrote: > 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... > > > |