3

Consider the multiple assignment x[0],y = y,x[0]. Applied to each of the four below cases, this gives four different results.

  • Case 1:

    x = [[1,2], [3,4]]
    y = [5,6]
    

    gives

    x = [[5,6], [3,4]]
    y = [1,2]
    
  • Case 2:

    x = np.array([[1,2], [3,4]])
    y = [5,6]
    

    gives

    x = array([[5,6], [3,4]])
    y = array([5,6])
    
  • Case 3:

    x = [[1,2], [3,4]]
    y = np.array([5,6])
    

    gives

    x = [array([5,6]), [3,4]]
    y = [1,2]
    
  • Case 4:

    x = np.array([[1,2], [3,4]])
    y = np.array([5,6])
    

    gives

    x = array([[5,6], [3,4]])
    y = array([5,6])
    

It appears that the multiple assignment of lists is smarter (going through a temporary variable automatically) than the multiple assignment of Numpy arrays.

Thoughts?

EDIT: It is not smarter afterall...

1
  • "It appears that the multiple assignment of lists is smarter (going through a temporary variable automatically) than the multiple assignment of Numpy arrays." -- How so? When assigning into a Numpy array, it turns that into part of the Numpy array. Commented Dec 15, 2016 at 9:35

2 Answers 2

5

The only surprising cases here should be 2 & 4:

x = np.array([[1,2], [3,4]])
y = np.array([5,6])  # or [5, 6]

giving

x = array([[5,6], [3,4]])
y = array([5,6])  # where did the 1 and 2 go?

Since the others are just swapping around data types, but keeping the same values.

What's different when using numpy is that x[0] returns a view, not a copy. So even writing out the temporary in the tuple assignment explicitly fails:

temp = x[0]
x[0] = y
y = temp

Because temp is a view that is always the same as x[0], not a copy of the value of x[0] at that point in time.

To make this work for the numpy case, you should use x[0],y = y,x[0].copy()

Sign up to request clarification or add additional context in comments.

1 Comment

Well, yes, 2 and 4 are the interesting ones but for completeness I added all of them.
1
x[0],y = y,x[0]

equals

t = x[0]
x[0] = y
y = t

As @Scimonster pointed out,

When assigning into a Numpy array, it turns that into part of the Numpy array.

So It's normal behavior.

3 Comments

It is not really true that these two code blocks are equal. Not temporary variable t is created; internally y and x[0] are pushed on to a stack, get flipped, and then are popped back off to be assigned. The two approaches can lead to different results.
@ajcr: Apart from the name t being clobbered, can you construct a case where the results do differ?
@Eric: I think I was mis-remembering something like this question that I came across a while ago (which I see now doesn't involve a temporary variable). I think it should be possible to construct strange examples that mutate variables inplace during the assignment (so leaving a temporary variable unchanged) but until I can come up with something compelling, I'll retract my previous comment.

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.