2

here's my problem. Sorry for the previous post which was not clear at all.

so here's an example:

import numpy as np

x=np.arange(1,100,1)
y=z=x*0

def func(h,g):
    for i in range(1,50):
        h[i]=i+1
        g[i]=i*2

func(z,y)
print z-y

In this example z and y give the same answer, but why is that so? In the function it is not supposed to give the same answer?

3
  • possible duplicate of Iterator (iter()) function in Python Commented Aug 13, 2013 at 16:53
  • @OP : You could also do this with np.zeros(...). Commented Aug 13, 2013 at 16:57
  • @PeterDeGlopper Y I'm not confused about that :) Commented Aug 13, 2013 at 17:00

1 Answer 1

4

You're setting y and z to both point to the same object. This line:

y=z=x*0

creates one new object, x*0, then sets both y and z to refer to it. Thus, h and g in your function are the same object, and the updates overwrite each other.

If you want to have two independent objects, create them independently:

y=x*0
z=x*0
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! i thought the y=z=x*0 was just a way to reduce the number of lines!
Nope. It literally sets y to the return value of z=x*0, which is z.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.