1

After defining an arrray a with zeros, I can create a view to the leftmost column with the following function:

a = np.zeros((5, 5))
a_left_col = a[:, 0]

a_left_col[:] = 2.

which prints for a:

array([[2., 0., 0., 0., 0.],
       [2., 0., 0., 0., 0.],
       [2., 0., 0., 0., 0.],
       [2., 0., 0., 0., 0.],
       [2., 0., 0., 0., 0.]])

If I subsequently reinitialize a with

a = np.zeros((5, 5))

then the view still exists, but it refers to nothing anymore. How does Python handle the situation if I do a_left_col[:] = 2 again? Is this undefined behavior like in C or C++, or does Python handle it properly, and if so, why doesn't it throw an error?

1
  • Reference counting. The original object a still exists because it is referenced by the view. (Although it can be no longer accessed through variable a.) Commented Jan 25, 2018 at 14:48

2 Answers 2

1

The original object still exists because it is referenced by the view. (Although it can be no longer accessed through variable a.)

Let's have a detailed look at the object's reference counts:

import sys
import numpy as np

a = np.zeros((5, 5))
print(sys.getrefcount(a))  # 2

a_left_col = a[:, 0]
print(sys.getrefcount(a))  # 3
print(sys.getrefcount(a_left_col.base))  # 3
print(a_left_col.base is a)  # True

a = np.ones((5, 5))
print(sys.getrefcount(a_left_col.base))  # 2

Note that a_left_col.base is the reference to the original array. When we reassing a The reference count on the object decreases but it still exists because it is reachable throgh a_left_col.

Sign up to request clarification or add additional context in comments.

4 Comments

Interesting. So in the case of views, I have to do memory management myself?
@Chiel No, it's all automatic. While there exist views into an array they count as references to the object. When an object is no longer referenced (no more views or other references) it will be deleted by the garbage collector at some point.
I get that, but since I tend to make a lot of views for shortcuts, it means that I have to delete all my views explicitly, in order to get my memory back. Is that correct?
@Chiel That is correct. However, that is usually only a problem in long scripts where variables stick around. When a function returns, all views that exist as local variables get deleted; and if you re-assign a variable (say, in a loop) you don't have to explicitly delete it either.
1

Behaviour is not undefined. You are merely creating a new object a. The old one is not deallocated, but still exists in memory, since a_left_col still references it. Once you reinitialize a_left_col, the original array can be deallocated.

Comments

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.