Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • well, then why id(foo) is also different from id(foo2)? do they use the first element 's address as their address? Commented Oct 25, 2013 at 23:56
  • 1
    @shelper: foo isn't foo2. Although they have the same shape, dtype, etc., and although they use the same storage for their elements, they are different objects. I don't think the ID you receive has any relation to the addresses of the array elements; it's the address of a header containing array metadata and a pointer to the storage used for the elements. Commented Oct 26, 2013 at 0:09
  • 1
    well, i think i understand the issue, foo and foo2 are both well wrapped python object, id(foo) just show the address of the python object, not the memory that contains the data, which actually can be get by " foo.__array_interface__['data'] " Commented Oct 26, 2013 at 0:10
  • 2
    You can check if two arrays share the same base memory by comparing their .base attribute, i.e. in your case foo.base is foo2.base should evaluate to True. Commented Oct 26, 2013 at 3:29
  • 1
    @Jamie - If I recall correctly, there can be cases where foo.base is not the same as foo2.base even though they share the same memory buffer. It holds true for all slicing operations, but not everywhere. Things that create a view through __array_interface__ (for example np.lib.stride_tricks.as_strided) won't necessarily show the same base. As I understand it, numpy.may_share_memory(foo, foo2) is preferred way to check if two arrays share the same memory (though I could be wrong there). Commented Oct 26, 2013 at 3:53