473,394 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Question from a beginner

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
Jul 18 '05 #1
3 1056
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" <rd******@bsc.edu> wrote in message
news:44**************************@posting.google.c om...
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

Jul 18 '05 #2

"Rodney Dunning" <rd******@bsc.edu> wrote in message
news:44**************************@posting.google.c om...
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.

Jul 18 '05 #3
Larry Bates wrote:
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)
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Svens | last post by:
Hey everyone! I'm a math student working on a short script involving logs. I have a function on my scientific calculator, and was wondering if there was a similar funtion in python. For example:...
10
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
4
by: a | last post by:
Dear all vb.net developer I want to know the time I need to master vb.net? I'm beginner
3
by: Ben Keshet | last post by:
I have a probably simple beginner's question - I have a script that I am currently able to print its output. instead, i want to write it into a file - I tried different versions of write() but...
2
by: roanhn | last post by:
Hello. I've to to write a master's thesis. Currently I deal with php, mysql, ajax. Fate decreed that I've to choose one of this subjects: 1.gdi+ library in .net technology 2.ado.net technology...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.