469,271 Members | 1,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

python references

>>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?

Feb 5 '07 #1
2 1076
du*********@gmail.com wrote:
>>>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
Feb 5 '07 #2

du*********@gmail.com wrote:
>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'
>x=10
now `x' is a reference to immutable integer object
with value 10, array doesn't change
>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?
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]]

--
HTH,
Rob

Feb 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

40 posts views Thread by Shufen | last post: by
53 posts views Thread by Michael Tobis | last post: by
18 posts views Thread by diffuser78 | last post: by
41 posts views Thread by none | last post: by
5 posts views Thread by Santiago Romero | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.