0

I am confused with a simple case about array variable assignment in Python, and hope some one could help me check it.

In my understanding, if a is a list, b just copied the reference of a, and when you edit b, a would be modified as well. Meanwhile, you could use the is operator to check their ids. For example:

a = ["a", ["a", "b"]]
b = a[1]
b.append("c")

Then, it would return True, when I use

In [7]: b is a[1]
Out[7]: True

However, if a and b are an arrays,

import numpy as np
a = np.identity(3)
b = a[0, :]

Afterwards, when I use is to check, it returns False, but when I edited b, a would be modified as well:

In [14]: b is a[1]
Out[14]: False
In [15]: a
Out[15]:
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
In [16]: b
Out[16]: array([0., 1., 0.])
In [17]: b *= 2
In [18]: b
Out[18]: array([0., 2., 0.])
In [19]: a
Out[19]:
array([[1., 0., 0.],
       [0., 2., 0.],
       [0., 0., 1.]])

Basically, I think if is returns False, variables would have different ids and references, which means that they are independent, but it seems wrong right now, could anyone help me to check it?

Many thanks!

2 Answers 2

1

Accessing a slice of the array creates a view. b and a[0,:] are distinct views, even though they view the same part of the array a, so their id values are different even though their underlying references are the same.

The id of an object is technically distinct from what it's referencing, so objects with the same id will reference the same data but objects with the same reference don't necessarily have the same id

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

2 Comments

Hi Michael, do you mean that only accessing a slice of array will create a view but not in accessing a slice of list? Meanwhile, I am not sure that how 2 variables with different ids could be modified at same time even though they have same reference. Is it a property of dynamic language?
Python lists don't have views, that's a Numpy feature. Two variables with the same reference are pointing to the same data in memory. So modifying that data from one reference is the same as modifying that data from the other, even if the two variables have different IDs.
0

Numpy arrays work differently than Python lists. In your example, b is a numpy view of the first row of a, which is not the same as a pointer to the first element a.

By the way, you can check the id of each of your variables by doing id(b) or id(a[0]) for example.

1 Comment

Hi Brian, I checked them too. I thought is operator has same meaning as checking id of variables. But I am confused that how can b and a[1] be modified together with different ids? Does this problem result from numpy or python?

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.