|
From: Robert X. <nne...@gm...> - 2009-09-13 00:01:30
|
In this particular case, vector() will accept another vector as an argument,
creating a copy of the vector. This allows you to dispense with the copy
module (which you should still know about):
>>> from visual import *
>>> v1 = vector(1,2,3)
>>> v2 = vector(v1)
>>> v1
vector(1, 2, 3)
>>> v2
vector(1, 2, 3)
>>> v1.x = 3
>>> v1
vector(3, 2, 3)
>>> v2
vector(1, 2, 3)
Robert
2009/9/12 Parker Swanson <Par...@li...>
> Hi Lenore (& Robert & the list),
>
> If you're used to passing parameters by value (as in languages like C), it
> can seem strange to pass them by reference (as with mutable objects in
> Python). You can dig into the Python docs to find out more about it.
>
> Python provides for copying mutable objects by value by its copy (and
> deepcopy) functions:
>
> >>> from copy import *
> >>> from visual import *
> >>> vector1=vector(1,2,3)
> >>> vector2=vector1 {copy by reference: vector2
> and vector1 are two names referring to the same object}
> >>> vector2
> vector(1, 2, 3)
> >>> vector2.y=4
> >>> vector2
> vector(1, 4, 3)
> >>> vector1
> vector(1, 4, 3)
> but:
> >>> vector3=vector(copy(vector1)) {copy by value: vector3 is a new
> vector: each of its components is a copy of the corresponding component of
> vector1}
> >>> vector3
> vector(1, 4, 3)
> >>> vector1.y=5
> >>> vector1
> vector(1, 5, 3)
> >>> vector3
> vector(1, 4, 3)
>
> The "clumsy process" you mention accomplishes the same thing in a piecewise
> fashion.
>
> Hope this helps! And I hope you enjoy using Python as much as thousands of
> the rest of us do. Best regards,
> Parker
>
>
> ----------------------------------------------------------------------------
>
> Message: 2
> Date: Sat, 12 Sep 2009 10:56:58 -0500
> From: Lenore Horner <lh...@si...>
> Subject: Re: [Visualpython-users] strange math
>
> 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
> >
>
>
> ------------------------------------------------------------------------------
> 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
>
|