Suppose I have a function like this:
def f():
x = np.arange(100)
return x[:5]
f returns a y, which is a view on x.
Will x still be using memory in the background?
Short answer: yes. Whilst x will be kept alive by your slice. See the documentation for basic slicing.
You should copy the view before returning.
return x[:5].copy()