PDA

View Full Version : Getting around Python's referencing of variables


Nevada
2007.03.10, 02:17 PM
I have a simple but very frustrating problem with Python. I know it should have a simple answer, but I'm just not familiar enough with Python to figure it out.

I need to do a 90 degree rotation of a velocity vector, but I'm having some trouble with the fact that Python, unlike C, treats assigned variables like pointers, rather than copies. My code is similar to this:

oldx = x
x = y
y = -oldx

The problem is that setting x = y also sets oldx = y, so I just get x = y and y = -y. Does anyone know how I can get a copy of a variable?

martinsm
2007.03.10, 02:57 PM
Your code can be written like:
x,y = y,-x

But what type are x and y?
You can also take a look at copy (http://docs.python.org/lib/module-copy.html) module.

Nevada
2007.03.10, 04:07 PM
Wow, I didn't even think of that. Thanks.