python references

D

dustin.getz

from Numeric import zeros
p=zeros(3)
p array([0,0,0])
p[0] 0
x=p[0]
x=10
p
array([0,0,0]) #actual behavior
#array([10,0,0]) #desired behavior

I want x to be a C++-esque reference to p[0] for convenience in a
vector3 class. i dont want accessor methods. i know python can do
this, but it's been a long time since I used it and am unsuccessful in
my googling and docreading. a little help please?
 
D

Diez B. Roggisch

from Numeric import zeros
p=zeros(3)
p array([0,0,0])
p[0] 0
x=p[0]
x=10
p
array([0,0,0]) #actual behavior
#array([10,0,0]) #desired behavior

I want x to be a C++-esque reference to p[0] for convenience in a
vector3 class. i dont want accessor methods. i know python can do
this, but it's been a long time since I used it and am unsuccessful in
my googling and docreading. a little help please?

Nope, python can't do this.

Diez
 
R

Rob Wolfe

from Numeric import zeros
p=zeros(3)
p array([0,0,0])
p[0] 0
x=p[0]

`x' is now a reference to immutable integer object
with value 0, not to first element of array `p'

now `x' is a reference to immutable integer object
with value 10, array doesn't change
array([0,0,0]) #actual behavior
#array([10,0,0]) #desired behavior

I want x to be a C++-esque reference to p[0] for convenience in a
vector3 class. i dont want accessor methods. i know python can do
this, but it's been a long time since I used it and am unsuccessful in
my googling and docreading. a little help please?

You can have such a reference to mutable objects.
Consider this:
p = [[0,0,0], [0,0,0]]
x = p[0] # reference to mutable list object
x[0] = 10
p
[[10, 0, 0], [0, 0, 0]]
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top