Question from a beginner

R

Rodney Dunning

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

Larry Bates

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
 
G

George Kinney

Rodney Dunning said:
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.
 
T

Tuure Laurinolli

Larry said:
Test1=vector(1,0,0)
Test2=vector(1,0,0)

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

The copy function from module copy can also be used to create copies of
objects.
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.

No, not all objects can be changed, but names can be rebound. For
example strings and integers are immutable.
str="abc"
a=str(1)

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

The function hasn't been redefined, the name str has been changed to
refer to a new object. Consider the following:

mystr = str
str = "foo"

and

mystr(1)
str(1)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,047
Members
47,646
Latest member
xayaci5906

Latest Threads

Top