Question from a beginner

George Kinney gk_2345 at yahoo.com
Wed May 26 20:28:02 EDT 2004


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

In general, no. Python doesn't have a built in constant type or modifier.

The other problem is a really common gotcha for newbies, you have created a
variable Test1, and a reference to it, Test2. They both point to the exact
same object in memory.

You could do:
    Test1 = vector()
    Test2 = vector()
and have separate, independant vars.








More information about the Python-list mailing list