Nicholas
Nicholas

Reputation: 2668

What is a Python buffer object pointing to the start of the array’s data?

A = np.arange(12)
B = A.reshape(3, 4)
A[0] = 42
print(B)
print(A)
print(np.may_share_memory(A, B))
print(A.data == B.data)

Running above code, I am surprised that print(A.data == B.data) returns False. It seems A and B are sharing some memory, and their first element should be shared. Then if numpy.ndarray.data is an object pointing to the start of the array’s data (as the document said), it is expected to produce the same result on A and B.

Upvotes: 2

Views: 752

Answers (1)

hpaulj
hpaulj

Reputation: 231385

I prefer __array_interface__ as a way of looking at the attributes, including data buffer address:

In [766]: A = np.arange(12)
In [767]: B = A.reshape(3,4)
In [768]: A[0] = 42
In [769]: A
Out[769]: array([42,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
In [770]: B
Out[770]: 
array([[42,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [771]: A.data
Out[771]: <memory at 0xb16ef5dc>
In [772]: B.data
Out[772]: <memory at 0xb1719cdc>
In [773]: A.__array_interface__
Out[773]: 
{'data': (156295616, False),
 'descr': [('', '<i4')],
 'shape': (12,),
 'strides': None,
 'typestr': '<i4',
 'version': 3}
In [774]: B.__array_interface__
Out[774]: 
{'data': (156295616, False),
 'descr': [('', '<i4')],
 'shape': (3, 4),
 'strides': None,
 'typestr': '<i4',
 'version': 3}

A.__array_interface__['data'][0] values do match

The documentation for A.data is:

Python buffer object pointing to the start of the array’s data

but to ordinary Python programmers that can be misleading. @ajcr's comment is better. There is a difference between 'buffer object' and the address of the arrays data buffer.

============

I haven't used the data attribute much. One of the few cases has been to create an array using the ndarray function

how can I specify the memory address of a Numpy array using ctypes?

In [806]: np.ndarray((4,),buffer=A.data, dtype=int, offset=12)
Out[806]: array([3, 4, 5, 6])
In [807]: np.ndarray((4,),buffer=B.data, dtype=int, offset=16)
Out[807]: array([4, 5, 6, 7])

================

A.data just prints its repr, and is just as non-informative as:

In [808]: o=object()
In [809]: o
Out[809]: <object at 0xb729fc90>

Upvotes: 1

Related Questions