0

In below example x is view of y. Why id(x[1,1]) and id(y[1,1]) are different? I experimented with array views, but I don't understand how it works.

import numpy as np
x=np.array([[1,2,3],[4,5,6],[7,8,9]])
y=x[0:2,0:2]
y.base # is x
id(x[1,1])
id(y[1,1])

3
  • 1
    id return the identity of the Numpy temporary object created from x[1,1]. It is not the address of the cell. Numpy stores data in a C array that is not visible from CPython. You cannot directly access cells from CPython, but only from temporary CPython objects wrapping the information. Regarding the base pointer of the array, you can get it using arr.__array_interface__['data'][0] Commented Nov 11, 2023 at 15:47
  • id(x[1,1]) can produce the same or different values each time it's called, depending on how the temporary object memory and id is recycled. So checking the base is the right thing to do (or __array_intercace__). A view does share the same dafa-buffer with its base. But neither stores list like references. Commented Nov 11, 2023 at 18:44
  • What are you trying understand about views? id is virtually useless when applied to numpy objects. Commented Nov 11, 2023 at 21:53

0

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.