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... |