0

I wrote the following program to test the behavior of Numpy array view:

import numpy as np

a = np.array(\[1, 2, 3, 4, 5, 6\])

b = a\[2:4\]

# print(type(a.data))

# print(dir(a.data))

print(a.data)
print(hex(id(a.data)))
print()

# print(type(b.data))

# print(dir(b.data))

print(b.data)
print(hex(id(b.data)))
print()

# print(hex(id(a.data)))

# print(hex(id(b.data)))

# print(b.base)

print()

But The address of ndarray's buffer displayed by “watch” in vscode debuger is always change,As shown in the following pictures.

vscode display1

vscode display2

vscode display3

vscode display4

vscode display5

It's also different from the function "print()" printed.

print() printed

In my opinion,I think the address printed by function print() is right.But I don't konw why this happend.

Who can tell me why.

  • because it is just allocated the next memory space at each runtime. – D.L Jan 11 '23 at 15:45
  • The `id` of the `data` memory view is not the `address` of the array's databuffer. – hpaulj Jan 11 '23 at 16:11
  • If you did: `x=b.data; print(x); print(hex(id(x)))`, you'd see consistent results between the two prints. But that tells you nothing about what databuffer `b` is using. `x` is a `memoryview` object, not the databuffer itself. – hpaulj Jan 11 '23 at 17:06

0 Answers0