[Python] Is OWNDATA a false negative in this case?

Discussão em 'Python' iniciado por Stack, Março 21, 2025.

  1. Stack

    Stack Membro Participativo

    I've been testing some operations in numpy to get a better grasp of what copy and view are. Then I ran into the following code:

    import numpy as np

    arr = np.array([[1,2,7],[3,4,9],[5,6,0]])

    sliced_arr = arr[:,0:2]

    reshaped = sliced_arr.reshape(6,)

    print(reshaped.flags)
    print(reshaped.base is arr)
    reshaped[0] = 100

    print(reshaped)
    print(arr)


    Output:

    C_CONTIGUOUS : True
    F_CONTIGUOUS : True
    OWNDATA : False
    WRITEABLE : True
    ALIGNED : True
    WRITEBACKIFCOPY : False

    False
    [100 2 3 4 5 6]
    [[1 2 7]
    [3 4 9]
    [5 6 0]]


    Which shows that reshaped behaves like a copy given it doesn't modify arr. How is it possible reshaped not to be a view of arr, if sliced_arr is a view of arr? Is that OWNDATA a false negative here? I would like to know what is going on under the hood in this case.

    Continue reading...

Compartilhe esta Página