Question from a beginner

Larry Bates lbates at swamisoft.com
Wed May 26 19:46:57 EDT 2004


It's all about Python's pointers and immutable objects.

Test1 and Test2 point to the same vector (not copies
of the same vector).

You need:

Test1=vector(1,0,0)
Test2=vector(1,0,0)

to get to "independent" instances of the vector object.

All objects (that I'm aware of) in Python can be
changed.  You can even overwrite Python's own
objects.  People do it all the time when first
learning.  Actually this comes in handy after you
grow accustomed to it.

str="abc"

then try to do

a=str(1)

and get an error because the str() function has been
redefined to a string containing "abc".

HTH,
Larry Bates
Syscon, Inc

"Rodney Dunning" <rdunning at bsc.edu> wrote in message
news:44c2a239.0405261525.5b22cb88 at posting.google.com...
> Hello,
>
> I've just picked up python to create some 3D visuals (using VPython)
> for a physics class I'm developing. While writing a program, I
> developed a "bug" that is reflected in the code below. When executed,
> *both* Test1 and Test2 are changed. Can someone explain this to me?
> Why is Test2 changed?
>
> from visual import *
> from __future__ import division
>
> ##Variable tester
>
> a = vector(1,0,0)
>
> Test1 = a
>
> Test2 = a
>
> print "Test1 = ",Test1
> print "Test2 = ",Test2
>
> Test1.x += 3
>
> print "Test1 = ",Test1
> print "Test2 = ",Test2
>
> ##End code
>
> I've programmed extensively in Fortran-90. Is there anything in python
> analogous to the PARAMETER keyword in Fortran-90, such as
>
> integer, PARAMETER :: i = 10  !*** the value of i cannot be changed,
> period.
>
> Thanks for your help.
>
> --
> Rodney Dunning
> Assistant Professor of Physics
> Birmingham-Southern College





More information about the Python-list mailing list